Re: destructing error
* michael.goossens@gmail.com:
used code:
#include "Vecmath.h"
#include <iostream>
using namespace std;
int main(){
Vector v = Vector(4.0, 4.0, 2.0);
cout << "(" << v.x << ", " << v.y << ", " << v.z << ")" << endl;
cout << "length: " << v.Length() << endl;
cout << "normalizing ..." << endl;
Vector vn = Normalize(v);
cout << "normalized" << endl;
cout << "(" << vn.x << ", " << vn.y << ", " << vn.z << ")" << endl;
cout << "length: " << vn.Length() << endl;
vn.x = 1.f; vn.y = 0.f; vn.z = 0.f;
Vector *v2 = new Vector();
Vector *v3 = new Vector();
CoordinateSystem(vn, v2, v3);
cout << "(" << vn.x << ", " << vn.y << ", " << vn.z << ")" << endl;
cout << "(" << (*v2).x << ", " << (*v2).y << ", " << (*v2).z << ")"
<< endl;
cout << "(" << (*v3).x << ", " << (*v3).y << ", " << (*v3).z << ")"
<< endl;
delete v;
delete vn;
delete (*v2);
delete (*v3);
return 0;
}
The error occurs at the delete statements at the end and reads:
1>c:\users\micha;l\documents\visual studio 2005\projects\renderwoman
\renderwoman\raytracer.cpp(22) : error C2440: 'delete' : cannot
convert from 'Vector' to 'void *'
1> No user-defined-conversion operator available that can
perform this conversion, or the operator cannot be called
same thing for line 22 to 25. Why does that happen?
Since v and vn have not been allocated by new, and are not pointers,
it's neither necessary nor possible to use delete.
Anyways, instead of explicit new and delete, which is darn difficult to
get right (matching every executed new with exactly one executed
delete), consider using smart pointers such as boost::shared_ptr, which
automate this.
And deleting a
pointer should that be done by (*pointer) or just pointer ...
delete applies to a pointer -- but see above.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?