1

As in the subject I need operator which will work with pointers so I do not have to call *a>*b but a>b. For example my operator << works with the pointers ok:

friend ostream& operator<< (ostream &wyjscie, Para const* ex){
    wyjscie << "(" << ex->wrt << ", " << ex->liczbaWystapien <<")"<< endl;
    return wyjscie;
}

but this one give me an error:

friend bool operator> (Para const *p1, Para const *p2){
        return p1->wrt > p2->wrt;
}

Error   1   error C2803: 'operator >' must have at least one formal parameter of class type
2
  • 1
    Is the error message not clear? Apr 30, 2013 at 2:05
  • Why do you have pointers? Apr 30, 2013 at 4:13

2 Answers 2

1

Unfortunately, there isn't a way to overload an operator with two pointer values. This has to do with the ambiguity of such an overloaded operator.

However, you can do this with references instead - but you'd still need to use the * operator if you want to keep pointers:

friend bool operator> (Para const &p1, Para const &p2){
    return p1.wrt > p2.wrt;
}
1
  • I have it like that but the problem is once template class gets string as T and the other time it gets Para* as T and then > is not comparing right thigs
    – Yoda
    Apr 30, 2013 at 2:11
1

Your overloaded << works because it is being called on an ostream object (ostream.operator<<()).

The pointer overload of operator < does not work because a pointer is not a class so the following is meaningless: (const Para*).operator<().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.