Jump to content
  • Advertisement
Sign in to follow this  
ganbree

Pointer Hell

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

A runtime error is occuring, this is a bit simplified! int main() { Class1 * Obj1; Class2 * Obj2; Obj1 = new Class1(); Obj2 = new Class2(); Obj2->Pointer(Obj1); Obj2->Activate(); } ... private: Class1 * Class2::PClass1; // Member of Class2 ... Class2::Pointer(Class1 * ClassPointer) { PClass1 = ClassPointer; } Class2::Activate() { PClass1->Function(); // this is just a public funtion of class 1 // If I comment the equivelent above line all runs fine // By the way ive commented the entirety of Function() so it must be outside // that. } What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Well you might want to try creating Obj2 before using it.

Obj1 = new Class1();
Obj1 = new Class1(); // <- should probably be Obj2 = new Class2()

Share this post


Link to post
Share on other sites
You are initializing Obj1 twice. I'll assume that this is an error in your post, instead of in your code. I also trust that this example is not typical of your code's design (references would be a far better option in this case).

What are the addresses of the two objects, what exact line is responsible for the error, and what are the values of the variables when the error appears.

Also, it would be easier if you could provide a complete minimal example (CME) which demonstrates the error (one that we could just copy-and-paste into a source file to test).

Share this post


Link to post
Share on other sites
lol, yes that was just a posting error.

Its a runtime error not a compile error

if you look at the comments ive shown where I think the error is. (or simply manifests itself)

What do you mean by 'references'?

Share this post


Link to post
Share on other sites
Quote:
Original post by ganbree
if you look at the comments ive shown where I think the error is. (or simply manifests itself)


Could you please provide us with the exact place where the error occurs, and the relevant values (such as pointers) at that time? This takes only a few seconds using a debugger, and is far more helpful and precise than hypotheses.

References. They have the advantage that you cannot create runtime errors if your ownership structure is correct.

Share this post


Link to post
Share on other sites


Could you please provide us with the exact place where the error occurs, and the relevant values (such as pointers) at that time? This takes only a few seconds using a debugger, and is far more helpful and precise than hypotheses.

> Im not sure how to do this

References. They have the advantage that you cannot create runtime errors if your ownership structure is correct.

> that sounds usefull

> Ill come back to this tommorow.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!