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

Recommended Posts

Please help me, here is the code: Vectorx3f *operator -(const Vectorx3f *u, const Vectorx3f *v) { return new Vectorx3f(u->x-v->x,u->y-v->y,u->z-v->z); } Error Log: Vectorx3f* operator-(const Vectorx3f*, const Vectorx3f*)' must have an argument of class or enumerated type.

Share on other sites
You cannot overload primitve types,and pointers to types count as primitives in this respect.

Pointers to stuff must obey the rules of pointer arithmetic.

Anyway, returning a pointer from a metohd like that is a bad idea, it could cause lots of memory leaks.

A better idea would be:

Vectorx3f operator -(const Vectorx3f &u, const Vectorx3f &v){return Vectorx3f( u.x - v.x, u.y - v.y, u.z - v.z );}

[edit: yeah they aren't pointers anymore, thanks jflanglois! ]

Share on other sites
You can't do that with pointers. You must use a reference.

Vectorx3f operator -(const Vectorx3f &u, const Vectorx3f &v)

:D

John

ok, I see...

Thanks guys...

Share on other sites
Quote:
 Original post by rip-offVectorx3f operator -(const Vectorx3f &u, const Vectorx3f &v){  return Vectorx3f(u.x-v.x,u.y-v.y,u.z-v.z);}

(replace -> with .)

jfl.

Share on other sites
Quote:
 Original post by krumYou can't do that with pointers. You must use a reference. Vectorx3f operator -(const Vectorx3f &u, const Vectorx3f &v):DJohn

Returning by reference is bad unless the operator is an assignment one, as the return value is a temporary, and using a temporary reference out of scope is undefined (I think).

Share on other sites
It is normal to implement binary arithmetic operators in terms of immediate arithmetic operators:

// The immediate operator is supposed to mutate the this-object:// "subtract rhs from self". A reference to self is returned so that the// operator can chain in the usual way. That chaining is rarely used in normal// code, but it is sometimes used, and will actually help with the other// operator implementation.// We accept a const reference because the thing we subtract doesn't change,// just the thing we subtract *from*; but we also have to accept an instance// (not a pointer) and we don't want to copy it.Vectorx3f& Vectorx3f::operator-=(const Vectorx3f& rhs) {  x -= rhs.x; y -= rhs.y; z -= rhs.z;  return *this;}// For the binary operator, we are yielding a separate value which represents// the difference between the inputs. That calls for returning by value. We// still accept by const reference for the reasons cited above.// What we will do is make a copy of the lhs, using the copy constructor,// subtract rhs from the copy with that immediate operator, and yield the result.Vectorx3f operator-(const Vectorx3f& lhs, const Vectorx3f& rhs) {  return Vectorx3f(lhs) -= rhs;  // The -= yields a reference, but by returning by value it is implicitly  // dereferenced for us. The compiler should be good enough to optimize away  // any copying - beyond the initial copy, which is necessary because we  // return a separate object.}

EDIT: Clearly I am the moron here. Thanks. :)

[Edited by - Zahlman on May 19, 2006 10:46:06 AM]

Share on other sites
Just to be completely clear...
Vectorx3f& Vectorx3f::operator-=(const Vectorx3f& rhs) {  x -= rhs.x; y -= rhs.y; z -= rhs.z;  return(*this); }`

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
18

• 12
• 12
• 9
• 12
• 37
• Forum Statistics

• Total Topics
631421
• Total Posts
2999994
×