Return an Object By Value
-
Greetings Colleagues, I have a quick question. How do you return an object by value? The following code:
MyObject MyClass::operator+(const MyClass & mcRhs) { //...do some processing MyClass mcNewObject(); return mcNewObject; }
results in the following error: c++ error C2664: No constructor could take the source type, or constructor overload resolution was ambiguous It says it can't convert it. Ironically, when I leave the parentheses off of the instantiation of mcNewObject, it works!? Can someone please explain. Thanks a million. Sincerely, BP -
Greetings Colleagues, I have a quick question. How do you return an object by value? The following code:
MyObject MyClass::operator+(const MyClass & mcRhs) { //...do some processing MyClass mcNewObject(); return mcNewObject; }
results in the following error: c++ error C2664: No constructor could take the source type, or constructor overload resolution was ambiguous It says it can't convert it. Ironically, when I leave the parentheses off of the instantiation of mcNewObject, it works!? Can someone please explain. Thanks a million. Sincerely, BP -
Thanks for replying, This is the actual function
CNumber CNumber::operator+(const CNumber & cnThisNumber) { if((m_bIsBlank) || (cnThisNumber.ReturnIsBlank())) { CNumber cnBlankNumber(); return cnBlankNumber; } else { float fReturnAmount; cnThisNumber.GetAmount(fReturnAmount); fReturnAmount = m_fAmount - fReturnAmount; CNumber cnNewAmount(fReturnAmount); return cnNewAmount; } }
This is the copy constructor I have defined...CNumber::CNumber(const CNumber & cnThisNumber) : m_iMaxDecimals(10) { // Set all changeable data to cnThisNumber cnThisNumber.GetAmount(m_fAmount); cnThisNumber.GetOriginalAmount(m_fOriginalAmount); m_bFormatChange = cnThisNumber.ReturnFormatChange(); m_bIsZero = cnThisNumber.ReturnIsZero(); m_strStringAmount = cnThisNumber.ReturnStringAmount(); m_bIsBlank = cnThisNumber.ReturnIsBlank(); }
If I change "CNumber cnBlankNumber()" in the operator overload to "CNumber cnBlankNumber" it works. I just don't get it. -
Thanks for replying, This is the actual function
CNumber CNumber::operator+(const CNumber & cnThisNumber) { if((m_bIsBlank) || (cnThisNumber.ReturnIsBlank())) { CNumber cnBlankNumber(); return cnBlankNumber; } else { float fReturnAmount; cnThisNumber.GetAmount(fReturnAmount); fReturnAmount = m_fAmount - fReturnAmount; CNumber cnNewAmount(fReturnAmount); return cnNewAmount; } }
This is the copy constructor I have defined...CNumber::CNumber(const CNumber & cnThisNumber) : m_iMaxDecimals(10) { // Set all changeable data to cnThisNumber cnThisNumber.GetAmount(m_fAmount); cnThisNumber.GetOriginalAmount(m_fOriginalAmount); m_bFormatChange = cnThisNumber.ReturnFormatChange(); m_bIsZero = cnThisNumber.ReturnIsZero(); m_strStringAmount = cnThisNumber.ReturnStringAmount(); m_bIsBlank = cnThisNumber.ReturnIsBlank(); }
If I change "CNumber cnBlankNumber()" in the operator overload to "CNumber cnBlankNumber" it works. I just don't get it. -
I don't get the () thing, but try doing
return CNumber;
orreturn CNumber();
. Also - do you have a default constructor defined? MikeI will try that, and yes I do have a default constructor defined. Thanks, BP
-
Greetings Colleagues, I have a quick question. How do you return an object by value? The following code:
MyObject MyClass::operator+(const MyClass & mcRhs) { //...do some processing MyClass mcNewObject(); return mcNewObject; }
results in the following error: c++ error C2664: No constructor could take the source type, or constructor overload resolution was ambiguous It says it can't convert it. Ironically, when I leave the parentheses off of the instantiation of mcNewObject, it works!? Can someone please explain. Thanks a million. Sincerely, BPBlitzPackage wrote:
It says it can't convert it. Ironically, when I leave the parentheses off of the instantiation of mcNewObject, it works!? Can someone please explain.
Because C++ is not C# or Java. Look up how you define an object in C++ using the default ctor.
-
Greetings Colleagues, I have a quick question. How do you return an object by value? The following code:
MyObject MyClass::operator+(const MyClass & mcRhs) { //...do some processing MyClass mcNewObject(); return mcNewObject; }
results in the following error: c++ error C2664: No constructor could take the source type, or constructor overload resolution was ambiguous It says it can't convert it. Ironically, when I leave the parentheses off of the instantiation of mcNewObject, it works!? Can someone please explain. Thanks a million. Sincerely, BP