error C2678: binary '<'

This is a discussion on error C2678: binary '<' within the C++ Programming forums, part of the General Programming Boards category; Hi All, I am carrying out operator overload for a class - ErrorType and I am getting this error on ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    error C2678: binary '<'

    Hi All,

    I am carrying out operator overload for a class - ErrorType and I am getting this error on compiling ErrorType.cpp:

    errortype.cpp(43) : error C2678: binary '<' : no operator found which
    takes a left-hand operand of type 'const ErrorType' (or there is no
    acceptable conversion)

    Here is the portion of code:

    Code:
    bool ErrorType::operator<(const ErrorType& right)
    {
    if((priority < right.priority)||
    (priority == right.priority &&
    timestamp < right.timestamp)||
    (priority == right.priority &&
    timestamp == right.timestamp &&
    errorCode < right.errorCode))
    return true;
    else
    return false;
    }
    
    
    bool ErrorType::operator>(const ErrorType& right)
    {
     return right < *this; //line 43
    }
    I will appreciate any assistance.

    Chike.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > return right < *this; //line 43
    I think it should be:
    return *this < right;

    Although I don't quite understand why.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by swoopy
    > return right < *this; //line 43
    I think it should be:
    return *this < right;

    Although I don't quite understand why.
    The parameter right was declared const. That means the object is const inside the operator function. But, const objects can only call const member functions because const objects must have a guarantee that they won't be changed. Since the operator<() function wasn't defined as const, e.g.
    Code:
    bool ErrorType::operator<(const ErrorType& right) const
    a const object can't call the function. The reason is that every class method has an invisible parameter: the this pointer. The this pointer can implicity or explicity be used inside the function to change the object, e.g.:

    setPrivateVar(10);

    or

    this->setPrivateVar(10);

    When you declare a member function const, the compiler will ensure that no changes can be made to the calling object.
    Last edited by 7stud; 10-22-2005 at 04:48 AM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    Thank you Swoopy,

    You got it. Although I had resolved the problem anoter way by removing the "const" qualifiers (which is against my design of making the members (arguments) unmodifiable. So now I went and read this up again at the msdn site and have now inserted back the "const" qualifiers and made the member functions const functions.

    Thanks a lot fo your very detailed explanation. I understand this better now.

    Chike.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by 7stud
    Since the operator<() function wasn't defined as const, e.g.
    Code:
    bool ErrorType::operator<(const ErrorType& right) const
    a const object can't call the function. The reason is that every class method has an invisible parameter: the this pointer. The this pointer can implicity or explicity be used inside the function to change the object, e.g.:

    setPrivateVar(10);

    or

    this->setPrivateVar(10);
    Ok, that makes sense. 7stud, thanks for the thorough explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21