I am making a three-dimensional vector class (called Vector3) in c++. Right now, I am trying to overload the stream insertion operator (<<) so that I can directly print all of the vector's components all at once. I copied the sintax from MSD, but I get an 3 errors.
error: passing 'const Vector3 ' as 'this' argument of 'float Vector3::getX()' discards qualifiers [-fpermissive]
error: passing 'const Vector3 ' as 'this' argument of 'float Vector3::getY()' discards qualifiers [-fpermissive]
error: passing 'const Vector3 ' as 'this' argument of 'float Vector3::getZ()' discards qualifiers [-fpermissive]
(differences are marked in bold)
In Vector3.h under public, I entered this function declaration:
friend ostream& operator<<(ostream &os, const Vector3 &vector);
In Vector3.cpp, I implemented it:
ostream& operator<<(ostream& os, const Vector3& vector)
{
os << "(" << vector.getX() << ", " << vector.getY() << ", " << vector.getZ() << ")" << endl;
return os;
}
It should print out something like (x, y, z) according to the x, y, and z variables.
On a side note, shouldn't the vector.getX() lines use the -> instead of the dot, because the vector object is an address pointer?
getX()aconstmember function? - hmjdoperator<<doesn't need to be afriendif it only calls public member functions ofVector3. - Blastfurnacevectorparameter is not a pointer (syntactically), it is a reference. Reference variables have the same "dot" syntax as value variables. - Rob I