When trying to attempt to overload operator<< in my shape.cpp, it can't seem to recognize the variables within the class, even though it is a friend of the class.
My .cpp file:
#include "Shape.h"
ostream& operator << (ostream& cout, const Shape& shapes)
{
for (int i = 0; i < points.size(); i++) //points is undefined
{
cout << points[i] << endl;
}
}
My .h file:
#include <vector>
#include "Point.h"
#pragma once
using namespace std;
class Shape
{
friend ostream& operator << (ostream& cout, const Shape& shapes);
private:
vector <Point> points; //Ordered list of vertices
};
I already used the same overload for my point class and it worked fine.
A
friendfunction is not a class member. It can be treated as any other non-class function. Except that, because it's afriend, it has access to its private members:It should now be clear that the object gets passed in as the
shapesparameter. Well, it'spointsmember must, therefore, beshapes.points.However, we can do even better than that. How about using range iteration?