Skip to main content
GameDev.net gamedev.net
🔒 Locked

[redacted anon]

Started by Jemburula May 4, 2005 at 4:15 AM 4 replies 1.3k views
Original Post
Jemburula
Jemburula

[redacted anon]

What we do in life... Echoes in eternity
dave
dave
I'm assuming you have a structure or class called Tile. Otherwise the compiler won't know what Tile is. This error is basically due to a spelling mistake unless u havent created the object.

Otherwise it should be fine.

Post your code on here inside [sourc e] [/sourc e] tags (with no space befpre the 'e'.

ace
Cygon
Cygon
Exactly, your variable 'NewTile' probably goes out of scope if it causes this error message. The pointer variable you declared behaves just like an ordinary int, only that it contains the address of a chunk of memory (namely an instance of your 'Tile' class) which will not be freed if you loose that address. In other words, while the memory you reserved is not bound to a scope, the pointer variable is.

Use it like this:
struct Tile { int a, b, c; };int main() {  Tile *NewTile = new Tile();    NewTile->a = 123;  delete NewTile;}


(Personally, if I had to educate someone about dynamic memory allocation, I wouldn't even mention there was a heap until the usage of new/delete had been covered completely, just assuming new takes memory out of thin air ;))

-Markus-

Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
cyph
cyph
Did you remember to #include the header file where you stored the class if outside the file where you create the new memory?

Eg if:

class foo{ int someInt;};


is in foo.h - then in your main.cpp file do:

#include "foo.h".........foo* pToFoo;pToFoo = new foo();


Also before deleting it check it exists, otherwise you will crash...

if(pToFoo){delete pToFoo;}


Think that should work! But its early so im hoping ;)

EDIT - wow fast posting o_O
Jemburula
Jemburula
Ah yeah I get it now thanks alot guys, yes I had included all that but did not know about the pointer going out of scope. :)
What we do in life... Echoes in eternity

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.