我试图覆盖一个类的<<
操作符。目的基本上是为我的课程实施类似toString()
的行为,以便将它发送到cout
将产生有用的输出。使用一个虚拟的例子,我有下面的代码。当我试图编译时,我得到了错误:C++操作符的多重定义<<
$ g++ main.cpp Rectangle.cpp
/tmp/ccWs2n6V.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)':
Rectangle.cpp:(.text+0x0): multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)'
/tmp/ccLU2LLE.o:main.cpp:(.text+0x0): first defined here
我不明白为什么会发生这种情况。我的代码如下:
Rectangle.h:
#include <iostream>
using namespace std;
class CRectangle {
private:
int x, y;
friend ostream& operator<<(ostream& out, const CRectangle& r);
public:
void set_values (int,int);
int area();
};
ostream& operator<<(ostream& out, const CRectangle& r){
return out << "Rectangle: " << r.x << ", " << r.y;
}
Rectangle.cpp:
#include "Rectangle.h"
using namespace std;
int CRectangle::area(){
return x*y;
}
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
main.cpp中:
#include <iostream>
#include "Rectangle.h"
using namespace std;
int main() {
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
*在成员定义的情况:*如果上面的'CRectangle'以及'超载<<'其他的类定义它使用'CRectangle'的'''然后有问题。即使在做“CRectangle”的前向声明!任何想法为什么? – Paschalis