overloading not

This is a discussion on overloading not within the C++ Programming forums, part of the General Programming Boards category; why when I try to compile this Code: test& operator~ (const test& b1, const test& b2); I get: error: ‚test& ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    overloading not

    why when I try to compile this

    Code:
     test& operator~ (const test& b1, const test& b2);
    I get:

    error: ‚test& Htest:perator~(const test&, const test&)‚ must take ‚void‚

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,785
    operator~ is a unary operator. Therefore, if it is overloaded as a member function, it must have take no arguments. If it is overloaded as a free function, it must take exactly one argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and does it return a test& or a test? a reference or a value?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,785
    Most likely it would return by value. (But of course there are exceptions for exceptional semantics.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    how about this one:

    Code:
    type& operator~=();
    I get this error:

    :35: error: expected primary-expression before ‚)‚ token
    :35: error: declaration of ‚operator~‚ as non-function

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,785
    operator~= is a binary function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so it should take two arguments you mean?

  8. #8
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    You cannot overload ~= because it's not a valid operator in C++. You can only overload (most of) the operators that already come with the language.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,785
    Quote Originally Posted by Ronix
    You cannot overload ~= because it's not a valid operator in C++. You can only overload (most of) the operators that already come with the language.
    Yes, that is correct.

    By the way, -EquinoX-, I really suggest that you post the smallest and simplest program that demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 12:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  3. Operator= overloading from another type
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2006, 08:15 PM
  4. Overloading operator ==
    By anon in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 03:26 PM
  5. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM

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