我有以下类(class),我将其与 cout
交友,现在我正尝试将其与 cin
交友,但出现错误...任何人都可以帮忙我,或者告诉我我做错了什么?
错误:
c:\mingw\bin../lib/gcc/mingw32/4.6.1/include/c++/bits/stl_algo.h:2215:4: error: passing 'const RAngle' as 'this' argument of 'int RAngle::operator<(RAngle)' discards qualifiers [-fpermissive]
类 RAngle
:
class RAngle
{
private:
int *x,*y,*l;
public:
int solution,prec;
RAngle(){
this->x = 0;
this->y = 0;
this->l = 0;
}
RAngle(int i,int j,int k){
this->x = &i;
this->y = &j;
this->l = &k;
}
friend istream& operator >>( istream& is, RAngle &ra)
{
is >> ra->x;
is >> ra->y;
is >> ra->l;
return is ;
}
}
最佳答案
没有足够的代码来回答您的问题。但是从错误我会说你,你的int RAngle::operator<(RAngle)
未定义为 const 方法,您在只有 const 的地方使用它。
此外,制作 operator<
并不是很好的实践或其他比较运算符返回 int,因为这可能会导致误解。此类运算符应返回 bool
.
所以,应该有这样的东西bool RAngle::operator<(const RAngle& other) const { /*...*/ }
.本主题涵盖 here和 here .
更新 这段代码完全陌生。为什么要使用指向 int
的指针?为什么要将某些数据设为私有(private)?施 worker 员 RAngle(int i,int j,int k)
不会像你想象的那样工作。
关于c++ - 将 '>>' 与自己的类(class)交友,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11012630/