Sign in to follow this  

using pointer with new and delete

This topic is 4075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok I've finally decided to start using new and delete for a new project of mine, but I'm getting alot of weird errors with stuff I thought should be working perfectly. basically I'm creating a one demensional array, of a size determined at runtime, and I need to read and write to the array. A little psuedo example of what I'm doing:

int *Array;

int width  = 5;
int height = 5;

Array = new int[width*height];

Array[2] = 6;


Am I doing this wrong, also I' getting weird undeclared indentifier errors, but I assume its cause by an error with the way I'm using pointers, as I've had it before and it went away after fixing a different error. If need bee I'll post the code, but that's pretty much what it is, except its a class split with a header and source file. thanks in advance.

Share this post


Link to post
Share on other sites
HEADER

#ifndef _MAP_H
#define _MAP_H
#include "graphicscore.h"
#include "inputcore.h"
#include <string>
#include <iostream>
#include <fstream>

class CMap
{
private:

CZolo_Graphics_Core *CGraphics;
CZolo_Input_Core *CInput;

int *m_ptextures;
int *m_pbasic_Layer;
int *m_pwater_Layer;

int m_width;
int m_height;

public:

void Init(CZolo_Graphics_Core *, CZolo_Input_Core *);
void Update();
void Display(int,int);
void Free();

void Load_Map(std::string);
};
#endif



SOURCE

#include "map.h"

using namespace std;

void CMap::Init(CZolo_Graphics_Core *graphics, CZolo_Input_Core *input)
{
CGraphics = graphics;
CInput = CInput;

}

void CMap::Display(int sx, int sy)
{
// draw the map at the given coordinates
for (int j=0; j<m_width; j++)
{
for (int k=0; k<m_height; k++)
{
// find the texture the tile is on and adjust the tile
// to get the right tile
int texture = m_pbasic_layer[k*j+j] / 64;
int tile = m_pbasic_layer[k*j+j] - (texture * 64);

int x1 = tile % 8 * 64;
int x2 = x1 + 64;
int y1 = tile / 8 * 64;
int y2 = y1 + 64;

CGraphics->Draw_Texture(m_ptextures[texture], (j*64+sx), (k*64+sy), x1,y1,x2,y2, 1,1, 255);
}
}
}

void CMap::Update()
{

}

void CMap::Free()
{
delete[] m_ptextures;
delete[] m_pbasic_Layer;
delete[] m_pwater_Layer;
}

void Load_Map(std::string filename)
{
// temp variable for reading in data when needed
std::string temp_string;
int temp_int;

// open file
fstream file;
file.open(filename.c_str());

// enter a while loop that will continue until the end
// of the file looking for map data

while (!file.eof())
{
file >> temp_string;

// read in basic data
if (temp_string == "[data]")
{
file >> m_width;
file >> m_height;

// create buffers for the map layers
m_pbasic_layer = new int[m_width*m_height];
m_pwater_layer = new int[m_width*m_height];

// read in texture data
file >> temp_int;
if (temp_int > 0)
{
// create texture buffer and load in each tilesheet
// the first tile sheet should always be a master sheet
m_ptextures = new int[temp_int];

for (int j=0; j<temp_int; j++)
{
file >> temp_string;
m_ptextures[j] = CGraphics->Load_Texture(temp_string);
}
}
}

// read in basic layer
if (temp_string == "[basic layer]")
{
for (int j=0; j<m_height; j++)
{
for (int k=0; k<m_width; k++)
{
file >> m_pbasic_layer[(k*m_width)+j];
}
}
}

// read in water layer
if (temp_string == "[water layer]")
{
for (int j=0; j<m_height; j++)
{
for (int k=0; k<m_width; k++)
{
file >> m_pwater_layer[(k*m_width)+j];
}
}

}
}
}



ERRORS

--------------------Configuration: Map - Win32 Debug--------------------
Compiling...
map.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(21) : error C2065: 'm_pbasic_layer' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(21) : error C2109: subscript requires array or pointer type
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(22) : error C2109: subscript requires array or pointer type
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(66) : error C2065: 'm_width' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(67) : error C2065: 'm_height' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(70) : error C2440: '=' : cannot convert from 'int *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(71) : error C2065: 'm_pwater_layer' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(71) : error C2440: '=' : cannot convert from 'int *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(79) : error C2065: 'm_ptextures' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(79) : error C2440: '=' : cannot convert from 'int *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(84) : error C2109: subscript requires array or pointer type
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(84) : error C2065: 'CGraphics' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(84) : error C2227: left of '->Load_Texture' must point to class/struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(96) : error C2109: subscript requires array or pointer type
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(96) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(108) : error C2109: subscript requires array or pointer type
C:\Program Files\Microsoft Visual Studio\MyProjects\Map\map.cpp(108) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
Error executing cl.exe.

Map.exe - 17 error(s), 0 warning(s)











Share this post


Link to post
Share on other sites
m_pbasic_Layer is defined in the class and you're using it as m_pbasic_layer in the methods (note capital L)

your Load_Map function has to be defined as:

CMap::Load_Map

Most of your errors are because of those two I mentioned. You should have MSDN open when you're developing with Visual Studio at all times. If you don't have MSDN integrated then you can browse it online and look for your errors codes yourself: searching for error XXXX will tell you why the error happened and what to look for. If you have MSDN integrated with visual studio you can click on the error line in your output window (where you're errors are listed) and hit F1 and you go straight to the error description page.

Share this post


Link to post
Share on other sites
In your header, you've got "m_pbasic_Layer". In your code file, you're trying to use "m_pbasic_layer". C++ is case-sensitive.

Edit: Beaten!!! My honor has been tainted!

Share this post


Link to post
Share on other sites
I feel like such an idiot, I can't belive I didn't notice that, two typos, caused 31 errors.

well thats what I get for writting code, with a case of insomnia at 3 in the morning.

thanks guys.

Share this post


Link to post
Share on other sites

This topic is 4075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this