# C++ really strange array issue

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

## 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&lt;3;p++)
{
for (i=0;i&lt;3;i++)
{
block.setX(curX);
block.setY(curY);
block.setWidth(TWidth);
block.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&lt;3;p++)
{
for (i=0;i&lt;3;i++)
{

printf("%d %d %d %d \n", block.getX(),block.getY(),block.getWidth(),block.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 on other sites
Quote:
 Original post by yaazzI 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 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 on other sites
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 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 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 classpublic:	...};

( 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 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 on other sites
No problemo (although I didn't realize you could do that in Java either)

1. 1
Rutin
19
2. 2
3. 3
JoeJ
15
4. 4
5. 5

• 22
• 19
• 11
• 13
• 17
• ### Forum Statistics

• Total Topics
631698
• Total Posts
3001768
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!