0

I have the following class, which I friended with cout and now I'm trying to friend it with cin but I get an error... Could anyone help me, or tell me what I've done wrong?

error:

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]

class 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 ;
    }
}
PermanentGuest
  • 5,213
  • 2
  • 27
  • 36
Bogdan M.
  • 2,161
  • 6
  • 31
  • 53
  • see if [this](http://stackoverflow.com/questions/5973427/error-passing-xxx-as-this-argument-of-xxx-discards-qualifiers) can help you – default Jun 13 '12 at 10:10
  • 5
    Given that the error is in `RAngle::operator<`, why not post that? I suspect the actual problem is that your operator overload should be marked `const`, and it is not. – Rook Jun 13 '12 at 10:10

1 Answers1

4

There is not enough code to answer your question. But from error I would say you, that your int RAngle::operator<(RAngle) is not defined as const method and you use it somewhere, where you have only const.

Also, it's not very good practive to make operator< or other comparison operators return int, because this may lead to misunderstanding. Such operators should return bool.

So, there sould be something like this bool RAngle::operator<(const RAngle& other) const { /*...*/ }. This topic is covered here and here.

Update This code is completely strange. Why use pointers to int? Why make some data private? Constructor RAngle(int i,int j,int k) will not work as you suppose.

Community
  • 1
  • 1
JustSomeGuy
  • 3,677
  • 1
  • 23
  • 31
  • 1
    His `opertor>>` is a bit strange too. I doubt that any pointers input from a stream will be usable. – James Kanze Jun 13 '12 at 10:56
  • Considering, that pointers do are not pointing to any allocated memory, yes. That code should crash. – JustSomeGuy Jun 13 '12 at 10:58
  • Undefined behavior, rather. Of course, if the file had been written by the same process, outputting pointers to objects which still exist, then it would be legal and work. But how useful is that? – James Kanze Jun 13 '12 at 11:30