Public Group

Argg!!! Classes!

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

Recommended Posts

Why doesn't this code work! Argg!!!!!!
void MapTiles::InitMapTiles(char *FileName,IDirect3DDevice9 *ag_d3d_device) {

aMapTile = new aTile*[MaxX];
for (int i=0;i==MaxX;i++) {
aMapTile = new aTile[MaxY];
}
//Get tile types of all and	assign coords to all tiles
for (int y=0;y==MaxY;y=y+1) {
for (int x=0;x==MaxX;x=x+1) {
aMapTile[x][y].mapX=x*32;
aMapTile[x][y].mapY=y*32;
}
}
}

Possible reasons I can come up with: Something to do with classes. It compiles ok, but when it gets to: "aMapTile[x][y].mapX=x*32;" it gives me an unhandled error exception. aMapTile is a struct defined inside the MapTiles class.
struct aTile {
float mapX,mapY;
float screenX,screenY;
int Type;
};
class MapTiles
{
public:

void InitMapTiles(char FileName[256],IDirect3DDevice9 *g_d3d_device);
aTile **aMapTile;
IDirect3DTexture9 **aMapTileTexture;
int MaxX,MaxY;
}

Anyone got any ideas?

Share on other sites
Quote:
 for (int i=0;i==MaxX;i++) { aMapTile = new aTile[MaxY]; }

The problem may lie in this for loop. Memory is never being allocated. i never equals MaxX. You probably wanted something like "i < MaxX"

Share on other sites
Why does i never equal maxX?

Doesn't that for loop mean:

Start with i=0. End when i equals MaxX. i = i +1 every turn.

Isn't that what it means? I am probably getting something confused with VB again =/

Share on other sites
Quote:
 Doesn't that for loop mean:Start with i=0. End when i equals MaxX. i = i +1 every turn.

NO! the second part of the for loop means run _until_ this contion fails. your check fails before it even runs once.

1st iteration -> i = 0; i is not == MaxX, so the contional fails and the loop ends.

Quote:
 Original post by MikeMJHThe problem may lie in this for loop. Memory is never being allocated. i never equals MaxX. You probably wanted something like "i < MaxX"

yep, both your for loops should be like:

for (int i=0;i < MaxX;i++) {	aMapTile = new aTile[MaxY];}//snip...for (int y=0;y < MaxY;y=y+1) {	for (int x=0;x < MaxX;x=x+1) {		aMapTile[x][y].mapX=x*32;		aMapTile[x][y].mapY=y*32;		FileInput.read((char *) &aMapTile[x][y].Type, sizeof(int));	}}

-me

Share on other sites
Ahh, there we go! I just changed == to != and it works now. Thanks, I keep on getting the simple stupid stuff mixed up. Now it works... somewhat, but I hopefully won't be making that stupid mistake before! So used to using do loops in VB, I am still treating for loops the same. Thanks all for your help!

Share on other sites
Using a != in a for loop is BAD. Always use <, <=, > or >=. All the others are bad practice.

For instance, consider this code snippet
for (int nTeller = 0; nTeller != 99; ++nTeller){    nTeller += 1;}

It will never reach the != 99 condition, and thus loops forever. However, if you use nTeller < 100, it would have stopped once it went beyong the 99.

Toolmaker

Share on other sites
operator != isn't always bad in for loops. Don't steer him wrong. When using iterators, it is the best way to know when you've reached the end.

for (vector<int>::iterator pos = v.begin(); pos != v.end(); ++pos)   ;

And speaking of BAD programming practices, never use i++ to increment! It is stupid. Always use ++i instead.

The difference is that ++i increments i and returns the result, whereas i++ creates a copy of i, increments the original, and returns the copy (the old value). This is unnecessary in this context and is much slower. Never use i++ unless you for some reason need the original value returned.

Share on other sites
im sure toolmaker just didnt want to show him that its usefull when using iterators, seeing as hes having problems with just for loops...

anyway, my question is, is it true that doing i++ is actually slower? i mean, isnt this a micro-optimization? i use i++ for every for loop in the 50 + files of my game...

Share on other sites
Quote:
 Original post by graveyard fillaanyway, my question is, is it true that doing i++ is actually slower? i mean, isnt this a micro-optimization? i use i++ for every for loop in the 50 + files of my game...

i++ is indeed slower, and it is indeed a micro-optimization, but since there's no loss of expressivity involved and it's a trivial thing to remember, it's good to get in the habit of using ++i.

Share on other sites
oh well... maybe for my next project then, lol

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633701
• Total Posts
3013445
×