Overloading the insertion operator
Trying to do some state reporting. I'm running into accessibility issues trying to print out objects.
The class:
1 2 3 4 5 6 7
|
class Triangle
{
private:
const Point *vertex_ref[3];
public:
friend ostream& operator<<(ostream &, const Triangle &);
}
|
The code in triangle.cpp:
1 2 3 4
|
ostream& operator<<(ostream &outstream, const Triangle &tri)
{
outstream << tri.vertex_ref[0];
}
|
The code in point.cpp
1 2 3 4 5 6
|
ostream &operator<<( ostream &outstream, const Point *p)
{
outstream << p->x << ", " << p->y << ", " << p->z;
return outstream;
}
|
It's telling me I can't access vertex_ref[0] in triangle.cpp, even though the member function is declared as friend. Not sure what is wrong.
Last edited on
Syntax is good other than you are missing a semi-colon at the end of your class define. Compiles fine for me.
1 2 3 4 5
|
class Triangle
{
}; // semi-colon
|
You can also do this just to keep it nice and tiddy:
1 2 3 4
|
ostream &operator<<( ostream &outstream, const Point *p)
{
return outstream << p->x << ", " << p->y << ", " << p->z;
}
|
Last edited on
Topic archived. No new replies allowed.