binary '<<': no operator found

Hi, I am getting this error

binary '<<' no operator found which takes a right-hand operand of type 'Point2D' (or there is no acceptable conversion)

Having done some research it says to #include <string>, I have included it however still unable to solve it. I do not understand why.

Point2D.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef Point2D_h
#define Point2D_h

class Point2D
{
public:
	int x;
	int y;
	Point2D(int _x, int _y);
	~Point2D();
};
#endif 


Point2D.cpp
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

#include "pch.h"
#include "Point2D.h"
#include <string>
#include <string.h>
#include <cstdlib>
#include <iostream>
using namespace std;

Point2DPoint2D(int _x, int _y)
{
	x = _x;
	y = _y;
}

Point2D~Point2D()
{

}

bool operator==(const Point2D& lhs, const Point2D& rhs)
{
	return lhs.x == rhs.x && lhs.y == rhs.y;
}
	
ostream& operator<<(ostream& os, const Point2D& point)
{
	os << "Point2D: " << " x=" << point.x << ", y=" << point.y;
	return os;
}


main.cpp
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

#include pch.h
#include "Point2D.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <string.h>

using namespace std;

int main() 
{
	vectorPoint2D points;
	points.push_back(Point2D(3, 3));
	points.push_back(Point2D(5, 5));
	points.push_back(Point2D(8, 8));

	Point2D tofind(5, 5);

	if (find(points.begin(), points.end(), tofind) != points.end()) 
	{
		cout << tofind << " is found" << endl; // ERROR HERE
		
	}
}
Last edited on
1. Which operator are you overloading?
 
ostream& operator(ostream& os, const Point2D& point)

2. You're not declaring the operator<<() overload in Point2D.h, so the overload is not visible from main.cpp.
@helios

I'm using the << operator, I've done some edits to my post above as there were some typos but my codes do have << operator.

So I have edited them as such:

Point2D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef Point2D_h
#define Point2D_h
#include <iostream>
using namespace std;
class Point2D
{
public:
	int x;
	int y;
	Point2D(int _x, int _y);
	
	~Point2D();
};

ostream& operator<<(ostream& os, const Point2D& point)
{
	os << "Point2D: " << " x=" << point.x << ", y=" << point.y;
	return os;
}


Point2D.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "pch.h"
#include "Point2D.h"
#include <string>
#include <string.h>
#include <cstdlib>
#include <iostream>
using namespace std;

Point2D::Point2D(int _x, int _y)
{
		x = _x;
		y = _y;
}

Point2D::~Point2D()
{

}


I'm not too sure if i am declaring them correctly but now Im getting :
binary '==': no operator found which takes a left-hand operand of type 'Point2D'
It not wrong to put the definition in the header, but your code will be more organized if you only put in the header those implementations that need to be in the header.
1
2
3
4
5
//header:
ostream& operator<<(ostream& os, const Point2D& point);

//source:
ostream& operator<<(ostream& os, const Point2D& point){ /*...*/ }
But, if you really want to have the definition in the header, you need to make it inline, otherwise you'll get linker errors:
 
inline ostream& operator<<(ostream& os, const Point2D& point){ /*...*/ }



now Im getting :
binary '==': no operator found which takes a left-hand operand of type 'Point2D'
std::find() uses operator==() to compare two objects for equality. You need to overload that operator for Point2D.
Topic archived. No new replies allowed.