Skip to main content
gamedev.net
🔒 Locked

operator error help

Started by mike25025 2 replies 400+ views
Original Post
mike25025
mike25025
im getting this error
error: passing `const SVector' as `this' argument of `SVector SVector::operator-(const SVector&)' discards qualifiers
in this code
SVector operator-(const SVector& v)
{
	return SVector(x-v.x,y-v.y,z-v.z);
}
i know theres a simple solution but i dont know what it is thanks for any help
Evil Steve
Evil Steve
SVector operator-(const SVector& v){	return SVector(x-v.x,y-v.y,z-v.z);}

Should be:
SVector operator-(const SVector& v) const{	return SVector(x-v.x,y-v.y,z-v.z);}

Note the additional const. This lets the compiler know that it's operating on a const object
Washu
Washu
Another note: You really should define operator- in terms of operator -=. In other words:

CVector operator-(CVector const& lhs, CVector const& rhs) {  return CVector(lhs) -= rhs;}
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Jingo
Jingo
Quote:
Original post by Washu
Another note: You really should define operator- in terms of operator -=. In other words:

CVector operator-(CVector const& lhs, CVector const& rhs) {  return CVector(lhs) -= rhs;}


You should really define operator- in terms of expression templates.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.