Problem deleting a class.

Started by
13 comments, last by Qw3r7yU10p! 19 years, 6 months ago
Hello, Here is an example of what I'm having trouble with:

myclass1 class1 = new myclass1;
myclass1 class2;

class2.CopyObject(class1);
delete class1; //This exits the program for no reason at all, no message or anything.

myclass1:

myclass1::CopyObject(myclass1 m)
{
   x = m.x;
   y = m.y;
   z = m.z;
   ...
}

Why does this exit my application? Thanks~ John DiSanti
My game development blog: http://rykerlabs.com
Advertisement
wtf? is the "myclass1::CopyObject(myclass1 m)" taking a pointer as an argument ?!

that code shouldn't even compile.

(it is C++ isn't it?)
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Where did you get a pointer?
There is no asterick in the function parameter.
If there is a class, it is C++.
My game development blog: http://rykerlabs.com
indeed. the delete operator only works with pointers, as does the new operator. applying them the way you do is considered very unfortunate. instead, it should be implemented something like this :
myclass1* class1 = new myclass1(); // default constructormyclass1* class2;class2.CopyObject(class1);delete class1;//------------- in the other filevoid myclass1::CopyObject(myclass1* m){x = m->x;y = m->y;z = m->z;}

the * indicates a pointer object (read the referenced tutorial). and make sure a return type is infront of your function (should be void). however, you would usually not be using a "CopyObject" method, implement it through either a copy constructor or overloading the '=' operator.
<edit :: fixed late-night grammatical mistakes>
- stormrunner
if that compiled, then you need a new compiler. try devc++
This code is incorrect:

Quote:Original post by stormrunner
myclass1* class1 = new myclass1(); // default constructormyclass1* class2;class2.CopyObject(class1);delete class1;//------------- in the other filevoid myclass1::CopyObject(myclass1* m){x = m->x;y = m->y;z = m->z;}



class2 should be a myclass1, not a myclass1*
daerid@gmail.com
Quote:Original post by disanti
If there is a class, it is C++.


Java isn't C++.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Quote:Original post by silvermace
Quote:Original post by disanti
If there is a class, it is C++.


Java isn't C++.


True. However, Java doesn't have the :: operator, does it?
daerid@gmail.com
Quote:Original post by daerid
Quote:Original post by silvermace
Quote:Original post by disanti
If there is a class, it is C++.


Java isn't C++.


True. However, Java doesn't have the :: operator, does it?


no, but thats not what you said, is it ;-)
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Quote:Original post by stormrunner
indeed. the delete operator only works with pointers, as does the new operator. applying them the way you do is considered very unfortunate. instead, it should be implemented something like this :
myclass1* class1 = new myclass1(); // default constructormyclass1* class2;class2.CopyObject(class1);delete class1;//------------- in the other filevoid myclass1::CopyObject(myclass1* m){x = m->x;y = m->y;z = m->z;}

the * indicates a pointer object (read the referenced tutorial). and make sure a return type is infront of your function (should be void). however, you would usually not be using a "CopyObject" method, implement it through either a copy constructor or overloading the '=' operator.
<edit :: fixed late-night grammatical mistakes>


...would result in a runtime-error

Where does class2 get its memory from ?

class2 = new myclass1();

Should be placed somewhere before calling CopyObject.

This topic is closed to new replies.

Advertisement