Re: operator function

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 05/28/04


Date: Fri, 28 May 2004 16:27:06 +0200

Kenny wrote:
>
> Its giving the same error:
> binary 'operator +' has too many parameters
> But I am passing only two parameters to be added
> why does it tell me this ?
>

Please don't top post. Put your reply beneath the text you
are replying to.

Seems I have fixed an error you have actually not seen right now :-)
This shows that it is always a good idea to tell us the exact error
message. Then we know what to look for.

> binary 'operator +' has too many parameters

Strange. There are a number of problems with your operator
(and with the GetTotalTime() function), but none
of them have anything to do with what the error message seems
to imply.

To remember, here is your operator

Setting operator+(const Setting &a, const Setting &b )
{
 return a.GetTotalTime() + b.GetTotalTime() ;

}

When compiling this, my compiler emits:

error C2662: 'GetTotalTime' : cannot convert 'this' pointer from 'const class Setting' to 'class
Setting &'
              Conversion loses qualifiers

Does that help you? Hint: A member function can be const. Under which circumstances
   is that important?

When fixing this, the next error in the operator is:

error C2664: '__thiscall Setting::Setting(const class Setting &)' : cannot convert parameter 1 from
'int' to
             'const class Setting &'
        Reason: cannot convert from 'int' to 'const class Setting'
        No constructor could take the source type, or constructor overload resolution was ambiguous

Hint: The compiler is talking about the result of the addition, which is an int. But the
operator returns a Setting object. In order to gap that bridge, the compiler needs to create
a Setting object from a single int, which requires a constructor that takes ...

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at


Relevant Pages

  • Re: operator function
    ... >> of them have anything to do with what the error message seems ... Setting (int h, int m, int s, int TT) ... Now the compiler knows, that it is safe to use that function on a const ... >> operator returns a Setting object. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: operator function
    ... Well I guess you are talking about a copy constructor right? ... The compiler is talking about the result of the addition, ... an int. ... > operator returns a Setting object. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Can some explain this explicit cast to me?
    ... > suspect it is to prime the compiler to treat the return value as int ... Hint: remove the cast and try to compile. ... The compiler will tell you why it is needed. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: It Pays to Enrich Your C Skills
    ... Check if you can score a perfect 10 (without using a compiler). ... int main{ ... struct bitfield { ... out if it is a negative integer constant or a constant expression ...
    (comp.lang.c.moderated)
  • OT: Re: Perl Peeves
    ... I see the result of a test being used as an int. ... the compiler just assumed you knew what you were doing ... introduced to the language later, so void * was unheard of in most code. ... This didn't mean bool was special, declaring it just signaled to the ...
    (comp.lang.perl.misc)