operator << overload not working

I have code that I have copied from a tutorial i am learning from. In the tutorial the code is working fine, but for me it is not, and i can not figure out why. The error is with the overloaded << operator, and the compiler is complaining that there is no operator found that takes a right hand operand of type const Point2D, but that is what I am trying to print? I do not understand. Errors on line 32, and 36

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #include <iostream>
#include <ostream>
#include <set>
#include <cmath>

class Point2D
{
private:
	double mX = 0.0;
	double mY = 0.0;
public:
	Point2D() = default;
	Point2D(double x, double y) : mX { x }, mY { y } {}

	double X() const { return mX; }
	double Y() const { return mY; }

	double Length() const { return std::hypot(mX, mY); }
};

inline bool operator < (const Point2D& p1, const Point2D& p2)
{
	return p1.Length() < p2.Length();
}

std::ostream& operator<<(std::ostream& os, const std::set<Point2D>& points) {
    os << '{';

    bool isFirst = true;
    for (const auto& p : points) {
        if (isFirst) {
            os << p;
            isFirst = false;
        }
        else {
            os << ", " << p;
        }
    }

    os << '}';
    return os;
}

int main()
{
	using std::cout;

	std::set<Point2D> points{ {20,30}, {22, 33}, {2,3} };
	cout << "Inital set of points: \n";
	cout << points;

}
Last edited on
You only defined the function to print a std::set<Point2D>, but you also need to define how to print a Point2D. The language doesn't know how to do this automatically.
Topic archived. No new replies allowed.