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. Returning Struct from a function

Returning Struct from a function

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++regex
8 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.
  • D Offline
    D Offline
    Don Guy
    wrote on last edited by
    #1

    Hello all, In my Header file: class MyData { struct Fparams { int i; bool flag; }; } Now in CPP file: void MyData::GetData() { Fparams fParam; fparam = AllData(); } MyData::Fparams MyData::AllData() { Fparams fparam; fparam.i = 5; fparam.flag = true; return fparam; } Now i get an error saying - error: no match for 'operator=' in fparam = AllData(); ........ What's wrong here? How can i fix this? Thanks in advance.

    A Richard Andrew x64R E 3 Replies Last reply
    0
    • D Don Guy

      Hello all, In my Header file: class MyData { struct Fparams { int i; bool flag; }; } Now in CPP file: void MyData::GetData() { Fparams fParam; fparam = AllData(); } MyData::Fparams MyData::AllData() { Fparams fparam; fparam.i = 5; fparam.flag = true; return fparam; } Now i get an error saying - error: no match for 'operator=' in fparam = AllData(); ........ What's wrong here? How can i fix this? Thanks in advance.

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      Make your nested structure public and declare the member functions in your class first then see what happens.

      1 Reply Last reply
      0
      • D Don Guy

        Hello all, In my Header file: class MyData { struct Fparams { int i; bool flag; }; } Now in CPP file: void MyData::GetData() { Fparams fParam; fparam = AllData(); } MyData::Fparams MyData::AllData() { Fparams fparam; fparam.i = 5; fparam.flag = true; return fparam; } Now i get an error saying - error: no match for 'operator=' in fparam = AllData(); ........ What's wrong here? How can i fix this? Thanks in advance.

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Well one problem is that in the AllData() function, you're returning a struct that is declared inside the function itself. This is no good because when the function exits, all the variables declared inside the function are destroyed, so you're returning something that no longer exists. It would be better if you wrote it as:

        void MyData::GetData()
        {
        Fparams fParam;
        AllData(fParam);
        }

        Void MyData::AllData(Fparams& params)
        {
        params.i = 5;
        params.flag = true;
        }

        The difficult we do right away... ...the impossible takes slightly longer.

        A D 2 Replies Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          Well one problem is that in the AllData() function, you're returning a struct that is declared inside the function itself. This is no good because when the function exits, all the variables declared inside the function are destroyed, so you're returning something that no longer exists. It would be better if you wrote it as:

          void MyData::GetData()
          {
          Fparams fParam;
          AllData(fParam);
          }

          Void MyData::AllData(Fparams& params)
          {
          params.i = 5;
          params.flag = true;
          }

          The difficult we do right away... ...the impossible takes slightly longer.

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #4

          You can return objects that are declared local to a function from a function. That's what copy constructors are for...

          1 Reply Last reply
          0
          • D Don Guy

            Hello all, In my Header file: class MyData { struct Fparams { int i; bool flag; }; } Now in CPP file: void MyData::GetData() { Fparams fParam; fparam = AllData(); } MyData::Fparams MyData::AllData() { Fparams fparam; fparam.i = 5; fparam.flag = true; return fparam; } Now i get an error saying - error: no match for 'operator=' in fparam = AllData(); ........ What's wrong here? How can i fix this? Thanks in advance.

            E Offline
            E Offline
            Erudite_Eric
            wrote on last edited by
            #5

            You need to override the = opperator for the struct so that all the data is copied back. Or use a pointer and pass it to the func, which is better, and a lot lighter on the stack.

            A 1 Reply Last reply
            0
            • E Erudite_Eric

              You need to override the = opperator for the struct so that all the data is copied back. Or use a pointer and pass it to the func, which is better, and a lot lighter on the stack.

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

              Looking at the error there's probably a missing equality operator, but why's not obvious as we haven't got the full class definition. And it's not better to pass a pointer rather than returning a value - a decent compiler C++98 compiler will implement NRVO which makes it just as efficient to return a value.

              E 1 Reply Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                Well one problem is that in the AllData() function, you're returning a struct that is declared inside the function itself. This is no good because when the function exits, all the variables declared inside the function are destroyed, so you're returning something that no longer exists. It would be better if you wrote it as:

                void MyData::GetData()
                {
                Fparams fParam;
                AllData(fParam);
                }

                Void MyData::AllData(Fparams& params)
                {
                params.i = 5;
                params.flag = true;
                }

                The difficult we do right away... ...the impossible takes slightly longer.

                D Offline
                D Offline
                Don Guy
                wrote on last edited by
                #7

                Thanks a lot everyone, it's solved

                1 Reply Last reply
                0
                • A Aescleal

                  Looking at the error there's probably a missing equality operator, but why's not obvious as we haven't got the full class definition. And it's not better to pass a pointer rather than returning a value - a decent compiler C++98 compiler will implement NRVO which makes it just as efficient to return a value.

                  E Offline
                  E Offline
                  Erudite_Eric
                  wrote on last edited by
                  #8

                  And the code ends up different to what you intend...

                  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