Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Unhandled exceptions, pointers and objects :(

Author
Message
Interplanetary Funk
14
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 31st Jan 2011 00:07
Hey, I'm having a problem with the following code:

It compiles fine but I get an unhandled exception error whenever I run the exe (debug or release) without giving me any useful information back. The problem seems to be this line:
Quote: "Player1->Character=*Steph;"


I honestly have no idea what I'm doing wrong as it compiles 100% fine.

I'm using visual studio 8 (2005) and the commercial version of darkGDK.

thanks in advance for any replies.

BASIC programmers never die, they GOSUB and don't RETURN.
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 31st Jan 2011 01:27 Edited at: 31st Jan 2011 01:31
And it will compile fine, if that is indeed the line causing the error. I have not compiled it but it does make sense...

In this case, "Player1->Character" is expecting a-pointer to an object of type _Character (_Character*). In many cases, that would mean prepending the & (address of) operator before the assignment when you create a variable of type _Character. However, you don't create a variable of type _Character, you are creating a variable of type (pointer-to) _Character (_Character*) with the new command. This means that the variable Steph is already a pointer, so by the line you are using, you are saying "assign the pointer to the address in memory that holds the address of the Steph object". In effect, you are assigning the pointer-to-a-pointer.

In short, remove the * operator before Steph and you will be assigning the pointer that it is expecting.

Don't forget to release the associated memory with the call to "delete Steph" before program exit!

JTK
Interplanetary Funk
14
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 31st Jan 2011 01:35 Edited at: 31st Jan 2011 01:35
OK, I've changed my code to the following:


however, I'm now getting the following compile error though:
Quote: ">.\Main.cpp(37) : error C2512: '_Player' : no appropriate default constructor available"


line 37 is simply "_Player Player1;"



BASIC programmers never die, they GOSUB and don't RETURN.
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 31st Jan 2011 02:12
Personally, I like your second version better.

It's an easy fix. Change your "_Player" struct like so and all works fine:



The reason why is that instead of storing a reference to an already existing _Character (which requires a default assignment of *something* - hence the need for the default constructor), you are actually copying the data (the compiler builds that for you).

JTK
Interplanetary Funk
14
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 31st Jan 2011 02:26
Ah, I see, I forgot I left the reference in there, thanks very much, it seems to work fine now

BASIC programmers never die, they GOSUB and don't RETURN.

Login to post a reply

Server time is: 2024-06-28 00:58:58
Your offset time is: 2024-06-28 00:58:58