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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,975
    Most likely it would return by value. (But of course there are exceptions for exceptional semantics.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,975
    operator~= is a binary function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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