Jump to content
  • Advertisement
Sign in to follow this  
CustomDrive

[SOLVED][C++] Problem with initializing classes...

This topic is 3629 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

For some weird reason, I seem to encounter a lot of weird problems when I try initializing an array of classes... Here's a sample of my code.
class Grid
{
	int		texNum;
};

class Object
{
	public:
		int	texNum;
		double	posX;
		double	posY;
		double	sizeX;
		double	sizeY;

		void set(int t, double x, double y, double sx, double sy)
		{
			texNum = t;
			posX = x;
			posY = y;
			sizeX = sx;
			sizeY = sy;
		};
};

Object          ObjMAP[MAX_OBJ];
Object	        WalMAP[MAX_WALL];

Grid		BGMAP[MAX_X][MAX_Y];

int main( int argc, char **argv )
{
	for(int c = MAX_X; c >= 0; c--){
		for(int d = MAX_Y; d >= 0; d--){
			BGMAP[c][d].texNum = 1;
		}
	}

        printf("%d",BGMAP[0][0].texNum);

	for(int c = MAX_WALL; c >= 0; c--){
		WalMAP[c].set(0,0,0,0,0);
	}
	
        printf("%d",BGMAP[0][0].texNum);

	for(int c = MAX_OBJ; c >= 0; c--){
		ObjMAP[c].set(0,0,0,0,0);
	}

        printf("%d",BGMAP[0][0].texNum);

	ObjMAP[0].set(3,100,200,50,50);

        printf("%d",BGMAP[0][0].texNum);

        return 0;
}


and the output I get from printf was "1000", which shows that something modified the values from BGMAP. Why does this happen? [Edited by - CustomDrive on July 15, 2008 7:22:21 AM]

Share this post


Link to post
Share on other sites
Advertisement
You are counting backwards on your arrays (you should probably be counting forewards).
And you start at MAX_blah instead of MAX_blah - 1 in all your loops. every loop is overwriting something.

Share this post


Link to post
Share on other sites
Instead of:
for(int c = MAX; c >= 0; --c){
You want:
for(int c = MAX-1; c >= 0; --c){
Or even better:
for(int c = 0; c < MAX; ++c){

[EDIT] KulSeran beat me to it ;)

Share this post


Link to post
Share on other sites
The above two posts are correct, but just to reiterate:
MAX_X and MAX_Y are not valid indices into your grid. Arrays in C++ are zero-based, meaning that for an array of size 20, the valid indices are in the range [0,19]. Similarly, an array of size MAX_X has valid indices [0,MAX_X-1].

Share this post


Link to post
Share on other sites
I don't know why it's excatly 1000, but you are writing past the ends of your arrays in all your loops.


int v[MAX];


this array has MAX elements, namely 0, 1, 2, ..., MAX - 1.
You are accessing v[MAX].

And why are you looping backwards?
And why do you use printf?
texNum is a private member? What compiler are you using that allowed this to compile?
More idiomatic code would look like this:


#include <iostream>
#include <algorithm>

class Grid
{
public:

int texNum;

Grid() { }
Grid(int texNum) : texNum(texNum) { }
};

class Object
{
public:
int texNum;
double posX;
double posY;
double sizeX;
double sizeY;

Object() { }
Object(int t, double x, double y, double sx, double sy)
: textNum(t), posX(x), posY(y), sizeX(sx), sizeY(sy) { }
};

Object ObjMAP[MAX_OBJ];
Object WalMAP[MAX_WALL];

Grid BGMAP[MAX_X][MAX_Y];

int main( int argc, char **argv )
{
std::fill(&BGMAP[0][0], &BGMAP[MAX_X][MAX_Y], Grid(1));

std::cout << BGMAP[0][0].texNum << std::endl;

std::fill(&WalMAP[0], &WalMap[MAX_WALL]);

std::cout << BGMAP[0][0].texNum << std::endl;

std::fill(&ObjMAP[0], &ObjMap[MAX_WALL]);

std::cout << BGMAP[0][0].texNum << std::endl;

ObjMAP[0] = Object(3,100,200,50,50);

std::cout << BGMAP[0][0].texNum << std::endl;
}




To further improve your code: don't use arrays. std::vector is safer.
And don't use globals. Use local variables or wrap them in objects and pass them where they ar eneeded.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!