Sign in to follow this  
yaazz

C++ really strange array issue

Recommended Posts

I have a class that contains coordinates for a square. I set the values, and then right afterwards, I try to access them ,and every index is the same as the last one! Anyone know why this is?
	tile block[3][3];	
	for (p=0;p<3;p++)
	{
		for (i=0;i<3;i++)
		{
			block[i][p].setX(curX);
			block[i][p].setY(curY);
			block[i][p].setWidth(TWidth);
			block[i][p].setHeight(THeight);
			printf("%d %d %d %d \n", block[0][0].getX(),block[0][0].getY(),block[0][0].getWidth(),block[0][0].getHeight());
			curX = curX+TWidth+20;
		}
		curX=50;
		curY= curY+THeight+10;
	}
	printf("%f %f %f",block[0][0].getRf(),block[0][0].getGf(),block[0][0].getBf());
	
	
	for (p=0;p<3;p++)
	{
		for (i=0;i<3;i++)
		{

			printf("%d %d %d %d \n", block[i][p].getX(),block[i][p].getY(),block[i][p].getWidth(),block[i][p].getHeight());
			printf("\n");
		}

	}

and the output
Quote:
50 50 50 50 120 50 50 50 190 50 50 50 50 110 50 50 120 110 50 50 190 110 50 50 50 170 50 50 120 170 50 50 190 170 50 50 1.000000 0.000000 0.000000190 170 50 50 ** *******Right here is where it starts outputting the numbers again after the loop ** 190 170 50 50 190 170 50 50 190 170 50 50 190 170 50 50 190 170 50 50 190 170 50 50 190 170 50 50 190 170 50 50
[Edited by - yaazz on May 9, 2007 8:03:48 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by yaazz
I have a class that contains coordinates for a square.
I set the values, and then right afterwards, I try to access them ,and every index is the same as the last one!
Anyone know why this is?


1) Use [ source ] tags to preserve whitespacing among other things.
2) Use iostreams instead of printf in C++
3) If this wasn't copied/pasted source or output (I suspect it isn't), please fix that. Transcribing code can lead to the introduction of new errors, and the accidental fixing of others. Just plain confusing &tm;.
4) See anything wrong with this picture? It's a butchered up version of the exact code you posted.

for (p=0;p<3;p++)
{
for (i=0;i<3;i++)
{
...
printf("%d %d %d %d \n", block[0][0].getX(),block[0][0].getY(),block[0][0].getWidth(),block[0][0].getHeight());
...
}
...
}


Of course, this is your first loop, not your second one, which means I'd expect your first loop to display only the same numbers -- in direct contraidction to your posted output -- and your second loop to be the one with varying numbers -- again, in direct contradiction to your posted output -- hence my suspicions I mentioned in point 3.

5) The whole get/set X/Y/Width/Height jiggle seems pointless here.

Share this post


Link to post
Share on other sites


1. I didnt know about the source tags, I will edit that
2.Printf is just faster to type out (its just for debugging purposes) so I used that
3. source/output pasted right out of the code/terminal
4.Yep and look at the Output, tile[0][0] is different every time :S
5.This is a sample from a larger program, hence the get/set methods

Share this post


Link to post
Share on other sites
Okay Post the source of tile. If that really is the output, it springs to mind that the second output section shows the last output of the first section as well -- meaning you did something stupid like shared X/Y/Width/Height between all instances of tile (are you abusing the static keyword, not knowing what it does in a class instance? Are you using globals for some unbeknownst reason?).

Props for actually copy/pasting though -- this puts you well ahead of many in terms of Asking Questions The Smart Way.

Share this post


Link to post
Share on other sites
tile.h

#ifndef TILE_H_
#define TILE_H_

class tile
{
public:
tile();
tile(int X, int Y, int W, int H);
void setX(int X);
void setY(int Y);
void setWidth(int H);
void setHeight(int W);

int getX();
int getY();
int getWidth();
int getHeight();

float getRf();
float getGf();
float getBf();

virtual ~tile();
};

#endif /*TILE_H_*/




tile.cpp

#include "tile.h"

int x,y,width,height;
float color[3]={1.0,0.0,0.0};
bool blocked = false;
int id;

tile::tile()
{
x=0;
y=0;
width=0;
height=0;
}

tile::tile(int X, int Y, int W, int H)
{
x=X;
y=Y;
width=W;
height=H;
}

tile::~tile()
{
}

void tile::setX(int X)
{
x=X;
}

void tile::setY(int Y)
{
y=Y;
}
void tile::setWidth(int W)
{
width=W;
}
void tile::setHeight(int H)
{
height=H;
}
int tile::getX()
{
return x;
}
int tile::getY()
{
return y;
}
int tile::getWidth()
{
return width;
}
int tile::getHeight()
{
return height;
}

float tile::getRf()
{
return color[0];
}
float tile::getGf()
{
return color[1];
}
float tile::getBf()
{
return color[2];
}




Yes I realize that I don't need the get/set since they are public

Share this post


Link to post
Share on other sites
class tile
{
public:
...
};

int x,y,width,height; <---these are globals, 1 instance of each of these variables is shared throughout the entire program

Looks like we've found the problem. The fix is easy enough:
class tile
{
int x,y,width,height; <---these are class members, 1 instance of each of these variables exists per specific instance of the tile class
public:
...
};


( sidenote: I'm using [ code ] tags here, but only because the snippets are small and they let me use HTML to make nice colored arrows for problem indicator -- I use [ source ] too for larger snippets )

Share this post


Link to post
Share on other sites
Oh so in C++ I have to put the variables for a class in the header file? I didn't know that, but it makes perfect sense.

Can you tell im a Java programmer? :P

Thank you very much for the help

Share this post


Link to post
Share on other sites

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