In the second case you are 'telling' to the compiler to use the overloaded << operator which has not been implemented.
In other words you can use the overloaded << operator outside its implementation only.
here are some simple files that will actually compile:
as you can see I put the const in the operator functions. it now complains:
error: passing 'const geom_Edge' as 'this' argument of 'geom_Point geom_Edge::getP1()' discards qualifiers
geogeplusplus: The second case is trying to use the operator<< defined for Point inside the operator<< defined for Edge, this should be ok?
The member functions getP1() and getP2() have to be declared const because you are attempting to access them through a const instance of geom_Edge. (const geom_Edge &e in your overloaded streaming operators)
Whenever you create a const instance of an object, it only has access to it's const member functions. Even if a non-const member function doesn't actually change the object, it is inaccessible from a const object.
If I may make a suggestion for you: Why are you prefixing all classes with "geom_"? It seems like you are implementing your own "home made namespace" scheme. Why don't you use an actual namespace instead?
Other than this little change, you dont have to change anything in your original code.
And you better stay way from "const" part in the declaration.
Because, your getP1() and getP2() functions return temporary objects on fly whenever they are called and your overloaded function with "const" qualification would prevent it complaining that "the function may not be called for a const or volatile qualified object".
A removal of const in the overloaded parameter declaration would not have any problem calling a temporary object from a temporary (non-const) parameter.
Normally we dont see a "function call" from overloaded << or >> operator functionality.
And noting that function calling from an overloaded << function is NOT recommended ( I guess, not allowed), you would have to access the data members of the called class directly.
First question of yours would be, how about private methods/data,
then, make the overloaded << function is a friend of your class, so that it would be able to access all of its methods and data including private and protected.
ostream & operator << (ostream &output, const geom_Point &p)
{
output << p.x << " " << p.y; // access them directly, as a friend
return output;
}
// geom_Edge.cpp
ostream & operator << (ostream &output, const geom_Edge &e)
{
output << e.P1 << " " << e.P2; // get them directly too, as a friend
return output;
}
Thats all. All should go good now.
Remember, here we made the overloaded operators are "friends" of the overriding classes hence the overloaded operator functionality can access all members of the classes including private data.
So you better declare the class parameters are of "const" qualified so that, even acceidently and unintentionally, the data would not be at loss/damage.
A non-const class parameter in an overloaded << or >> function and it is a friend of the called class, is a design issue. Not at all recommended.
And last not least, in any case, you have to use or call functions (like you noted in the first post), then the work-around I suggested in the last post can help you. In such case, const would be removed.