Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Return an Object By Value

Return an Object By Value

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BlitzPackage
    wrote on last edited by
    #1

    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

    M A B 3 Replies Last reply
    0
    • B BlitzPackage

      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

      M Offline
      M Offline
      Mike_V
      wrote on last edited by
      #2

      Can you post the definitions of the two classes? Or... what line was the error on? Mike

      B 1 Reply Last reply
      0
      • M Mike_V

        Can you post the definitions of the two classes? Or... what line was the error on? Mike

        B Offline
        B Offline
        BlitzPackage
        wrote on last edited by
        #3

        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.

        M 1 Reply Last reply
        0
        • B BlitzPackage

          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.

          M Offline
          M Offline
          Mike_V
          wrote on last edited by
          #4

          I don't get the () thing, but try doing return CNumber; or return CNumber();. Also - do you have a default constructor defined? Mike

          B 1 Reply Last reply
          0
          • M Mike_V

            I don't get the () thing, but try doing return CNumber; or return CNumber();. Also - do you have a default constructor defined? Mike

            B Offline
            B Offline
            BlitzPackage
            wrote on last edited by
            #5

            I will try that, and yes I do have a default constructor defined. Thanks, BP

            1 Reply Last reply
            0
            • B BlitzPackage

              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

              A Offline
              A Offline
              Anonymuos
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • B BlitzPackage

                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

                B Offline
                B Offline
                bob16972
                wrote on last edited by
                #7

                MyClass mcNewObject(); is a function declaration I think (Mike Dunn pointed that out to me via this forum when I did a similar thing a while back when trying to test constructor behavior a while back. Silly me :-O )

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups