c++ - 将 '>>' 与自己的类(class)交友

标签 c++ operator-overloading

我有以下类(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 { /*...*/ } .本主题涵盖 herehere .

更新 这段代码完全陌生。为什么要使用指向 int 的指针?为什么要将某些数据设为私有(private)?施 worker 员 RAngle(int i,int j,int k)不会像你想象的那样工作。

关于c++ - 将 '>>' 与自己的类(class)交友,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11012630/

相关文章:

c++ - “send to”如何管理输入参数? ( Windows )

c# - 如何重载泛型类的运算符?

c++ - 使用单 channel 纹理(OpenGL 2)?

c++ - 不稳定的分析时间

c++ - 带有 static_assert 的漂亮 sfinae

c++ - 请帮助我理解链表C++中的运算符重载=

c++ - C++ 错误中的 Operator() 重载和继承

python - 如何解决clang的Python绑定(bind)加载错误?

c++ - 子类重载基类赋值运算符导致歧义赋值错误

python - 在 __add__ 运算符中返回相同子类的对象