Bizzare code results!!

Started by
25 comments, last by Zahlman 16 years, 3 months ago
Well I tried out this and it didn't work,
int i = 0;	for (int y = 0; y < _height; y++)	{		for (int x = 0; x < _width; x++)		{			int tile = newMap[y][x];			_tiles[i++] = new Tile(x, y, Gfx::GetTexture(tile), tile);		}	}
d I tried it without 'new' and it just created a link error.
Advertisement
Organizing source files.

Link errors have, generally speaking, nothing to do with compile errors. Anyway, of course you don't write "new" there, because you want to store (and create) objects, not pointers to objects.
Yeah I know the difference, I just put it there in case someone asked.. Anyway with that loop I showed how can I get it to work with the constructor?
You're debugging optimized code. That's why the debugger is having trouble evaluating auto variables.

If you want it to work, make sure /Od is set (disable optimization). It doesn't matter whether you're linking against the release or debug CRT in this case.
The old way you were creating Tiles in your Field class was wrong.. you had a member variable who was a pointer (that's about the same size as an integer) but you were never allocating space for the tiles but you tried to call Init() on this empty pointer. You have two choices, whether to declare the _tiles variable with a static size (no *) and then they will be created at the same time as the Field class, or you can keep the pointer and allocate them with a new somewhere, this way you can have dynamic sizes. Don't forget to delete them at the Field destructor if you choose to use this :).
Optimization was disabled.

Quote:
The old way you were creating Tiles in your Field class was wrong.. you had a member variable who was a pointer (that's about the same size as an integer) but you were never allocating space for the tiles but you tried to call Init() on this empty pointer. You have two choices, whether to declare the _tiles variable with a static size (no *) and then they will be created at the same time as the Field class, or you can keep the pointer and allocate them with a new somewhere, this way you can have dynamic sizes. Don't forget to delete them at the Field destructor if you choose to use this :).


I think I know what you mean, I attempted to use new but it didnt work how can I fix this? A nice code example would be awesome :D

-thanks so far though
Quote:Original post by namingway
Yeah I know the difference, I just put it there in case someone asked.. Anyway with that loop I showed how can I get it to work with the constructor?


...

When I said "don't write 'new' there", I meant exactly that. All you do is get rid of the word "new".

_tiles[i++] = Tile(x, y, Gfx::GetTexture(tile), tile); // look ma, no "new"!


This says "make a Tile from these values, and put it in _tiles[i++]". To fix the link error, read the article.

...I think I may have to add "new" to the list of "forbidden to beginners" keywords in my signature... :/ Speaking of which, now is probably a good time to get rid of the '_map' member entirely (along with all the code that deals with it). Now that you have proper Tiles, you won't be needing it.

This topic is closed to new replies.

Advertisement