C++ 友元重载运算符 <<
c++ friend overloading operator <<
我正在尝试重载运算符 << 但我一直遇到此错误。我尝试做研究但没有结果。我有一个 Point2D.h 和一个 Point2D.cpp 有一个要重载的朋友函数。以下是我的代码:
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
下面是我的错误信息,不知道为什么没有匹配的方法
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)
你需要
#include <iostream>
或至少
#include <ostream>
2 其他建议:
- 包含守卫(
ifndef
、define
和 endif
)应该在 header 的最开始和最后 文件(endif
不得在源文件中,而应在 header 文件中)
- 在 header 中添加
using namespace std;
是不好的做法。至少在 header 中使用 std::
前缀。您可以选择是否在源代码中使用 using namespace std;
。我不会,但这是我个人的选择。
您需要添加另一个 header
#include <iostream>
不过 #include <ostream>
就够了。
正确的代码(见代码中的注释):
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
首先移动指令
#endif
从文件Point.cpp
到文件末尾Point2D.h
这些文件看起来像
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
并替代
#include <iomanip>
为了
#include <iostream>
或至少添加 header
#include <iostream>
到定义运算符的模块文件。
运算符也应该像这样声明
ostream& operator<< (ostream &out, const Point2D &pt);
否则,每次使用运算符时,都会创建 Point2D 类型的临时 object。
我正在尝试重载运算符 << 但我一直遇到此错误。我尝试做研究但没有结果。我有一个 Point2D.h 和一个 Point2D.cpp 有一个要重载的朋友函数。以下是我的代码:
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
下面是我的错误信息,不知道为什么没有匹配的方法
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)
你需要
#include <iostream>
或至少
#include <ostream>
2 其他建议:
- 包含守卫(
ifndef
、define
和endif
)应该在 header 的最开始和最后 文件(endif
不得在源文件中,而应在 header 文件中) - 在 header 中添加
using namespace std;
是不好的做法。至少在 header 中使用std::
前缀。您可以选择是否在源代码中使用using namespace std;
。我不会,但这是我个人的选择。
您需要添加另一个 header
#include <iostream>
不过 #include <ostream>
就够了。
正确的代码(见代码中的注释):
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
首先移动指令
#endif
从文件Point.cpp
到文件末尾Point2D.h
这些文件看起来像
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
并替代
#include <iomanip>
为了
#include <iostream>
或至少添加 header
#include <iostream>
到定义运算符的模块文件。
运算符也应该像这样声明
ostream& operator<< (ostream &out, const Point2D &pt);
否则,每次使用运算符时,都会创建 Point2D 类型的临时 object。