c ++的多个定义operator< [英] c++ multiple definitions of operator<<
本文介绍了c ++的多个定义operator<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图覆盖类的<<
运算符。目的基本上是为我的类实现一个 toString()
的行为,所以发送到 cout
会产生有用输出。使用虚拟示例,我有下面的代码。当我试图编译,我得到以下错误:
$ g ++ main.cpp Rectangle.cpp
/ tmp /ccWs2n6V.o:在函数`operator< <(std :: basic_ostream Rectangle.cpp :(。 0x0):多个定义`operator< <(std :: basic_ostream /tmp/ccLU2LLE.o:main.cpp :(。text + 0x0):首先定义这里
我不知道为什么会发生。我的代码如下:
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&运算符<<(ostream& out,const CRectangle& r){
return out< Rectangle:<< r.x<< ,< r.y;
}
Rectangle.cpp:
#includeRectangle.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>
#includeRectangle.h
using namespace std;
int main(){
CRectangle rect;
rect.set_values(3,4);
cout<< area:< rect.area();
return 0;
}
解决方案
strong>一个定义规则。快速修复是:
inline ostream&运算符<<(ostream& out,const CRectangle& r){
return out< Rectangle:<< r.x<< ,< r.y;
}
其他是:
- 在头文件中声明操作符,并将实现移动到
Rectangle.cpp
文件。 - 在类定义中定义运算符。
。
class CRectangle {
private:
int x,y;
public:
void set_values(int,int);
int area();
friend ostream&运算符<<(ostream& out,const CRectangle& r){
return out< Rectangle:<< r.x<< ,< r.y;
}
};
奖金:
- 使用包含守卫
- 使用标题中的命名空间std; 删除
。
I am attempting to override the <<
operator for a class. The purpose is basically to implement a toString()
like behavior for my class, so that sending it to cout
will produce useful output. Using a dummy example, I have the code below. When I attempt to compile, I get the foollowing error:
$ 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
I can't figure out why this is happening. my code is below:
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;
}
解决方案
You're breaking the one definition rule. A quick-fix is:
inline ostream& operator<<(ostream& out, const CRectangle& r){
return out << "Rectangle: " << r.x << ", " << r.y;
}
Others are:
- declaring the operator in the header file and moving the implementation to
Rectangle.cpp
file. - define the operator inside the class definition.
.
class CRectangle {
private:
int x, y;
public:
void set_values (int,int);
int area ();
friend ostream& operator<<(ostream& out, const CRectangle& r){
return out << "Rectangle: " << r.x << ", " << r.y;
}
};
Bonus:
- use include guards
- remove the
using namespace std;
from the header.
这篇关于c ++的多个定义operator<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文