问题
I am trying overload the operator << but i keep having this error. I try doing research but with no result. I have a Point2D.h and a Point2D.cpp with a friend functions to overload. Below are my codes:
Point2D.h
#include <string>
#include <iomanip>
using namespace std;
#ifndef Point2D_H
#define Point2D_H
class Point2D
{
friend ostream& operator<< (ostream&, Point2D);
public:
Point2D();
Point2D(int, int);
protected:
int x;
int y;
};
Point.cpp
#include <string>
#include <cmath>
#include <iomanip>
#include "Point2D.h"
Point2D::Point2D() {
this->x=0;
this->y=0;
}
Point2D::Point2D(int x, int y) {
this->x=x;
this->y=y;
}
ostream& operator<< (ostream &out, Point2D pt)
{
out << "Point = " <<pt.x;
return out;
}
#endif
Below are my error message, not sure why there is no match for that method
Point2D.h: In function ‘std::ostream& operator<<(std::ostream&, Point2D)’:
Point2D.h:37:9: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘int’)
out << pt.x;
^
Point2D.h:37:9: note: candidates are:
Point2D.h:35:10: note: std::ostream& operator<<(std::ostream&, Point2D)
ostream& operator<< (ostream &out, Point2D pt)
^
Point2D.h:35:10: note: no known conversion for argument 2 from ‘int’ to ‘Point2D’
In file included from Point2D.h:2:0,
from Point3D.h:2,
from Point3D.cpp:2:
/usr/include/c++/4.8/iomanip:235:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setw)
operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
回答1:
You need
#include <iostream>
Or at least
#include <ostream>
2 other advises:
- the include guards (
ifndef
,define
andendif
) should be at the very beginning and at the very end of the header file (theendif
MUST NOT be in the source file, but in the header file) - Adding
using namespace std;
in headers is bad practice. Usestd::
prefix at least in the header. It's your choice if you'll useusing namespace std;
in the source. I wouldn't, but it's my personal choice.
回答2:
You need to include another header
#include <iostream>
Only #include <ostream>
will suffice though.
回答3:
Correct code (see comments in code):
Point2D.h:
#include <string>
// #include <iomanip> // this includes io manipulators you do not need here
#include <iosfwd> // minimalist forward declarations for io streams
// using namespace std; // don't do this :(
#ifndef Point2D_H // should be above the includes
#define Point2D_H // should be above the includes
class Point2D
{
// friend ostream& operator<< (ostream&, Point2D);
friend std::ostream& operator<< (std::ostream&, const Point2D &);
// observe pass by const reference
Point2D.cpp
#include <string>
#include <cmath>
// #include <iomanip> // not needed
#include <iostream> // std::ostream class definition
#include "Point2D.h"
Point2D::Point2D() {
this->x=0;
this->y=0;
}
Point2D::Point2D(int x, int y) {
this->x=x;
this->y=y;
}
// ostream& operator<< (ostream &out, Point2D pt)
std::ostream& operator<< (ostream &out, const Point2D& pt)
{
out << "Point = " << pt.x;
return out;
}
// #endif // this shouldn't be here
回答4:
First of all move directive
#endif
from the file Point.cpp
to the end of the file Point2D.h
The files will look like
Point2D.h
#include <string>
#include <iomanip>
using namespace std;
#ifndef Point2D_H
#define Point2D_H
//...
#endif
Point.cpp
#include <string>
#include <cmath>
#include <iomanip>
#include "Point2D.h"
Point2D::Point2D() {
this->x=0;
this->y=0;
}
Point2D::Point2D(int x, int y) {
this->x=x;
this->y=y;
}
ostream& operator<< (ostream &out, Point2D pt)
{
out << "Point = " <<pt.x;
return out;
}
// #endif - removed
And substitute
#include <iomanip>
for
#include <iostream>
or at least add header
#include <iostream>
to the module file where the operator is defined.
Also the operator should be declared like
ostream& operator<< (ostream &out, const Point2D &pt);
Otherwise each time when the operator will be used a temporary object of type Point2D will be created.
来源:https://stackoverflow.com/questions/30392277/c-friend-overloading-operator