Re: operator function

From: Kenny (lerameur_at_yah00.c0m)
Date: 05/28/04


Date: Fri, 28 May 2004 10:47:58 -0400


"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:40B74C3A.E55B4D36@gascad.at...
> 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?

Well I guess you are talking about a copy constructor right?

>
> 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 ...

well this part I can solve by making the function void and just print out
the value so it will not return anything.
 cout<< a.GetTotalTime() + b.GetTotalTime();

>
> --
> Karl Heinz Buchegger
> kbuchegg@gascad.at



Relevant Pages

  • Re: strange compiler message
    ... >> int main ... strcpy(blah, " blah blah "); ... When I use the wrong signature the compiler issues the following error: ... All of this because C++ introduces the concept of calling a constructor ...
    (comp.lang.cpp)
  • Re: Working with strings in c++
    ... int i; ... The use of a copy constructor to initialise myStr from the temporary ... compiler to do it the two-step way, or the compiler is allowed to refuse to ... explicit MyString(const char* arg) ...
    (microsoft.public.vc.language)
  • 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: Requesting sample code!
    ... int m_i1, m_i2; ... then a constructor is not made automatically by the compiler. ...
    (microsoft.public.vc.language)
  • Re: operator function
    ... Hint: The compiler is talking about the result of the addition, ... a Setting object from a single int, which requires a constructor that takes ... ...
    (alt.comp.lang.learn.c-cpp)