Returning struct from function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Returning struct from function

  1. #1
    Join Date
    Aug 2012
    Posts
    53

    Returning struct from function

    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.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,131

    Re: Returning struct from function

    When you return fparam, you return a copy, and the compiler doesn't know how to copy it. You'll need to define an = operator. More typically though, you'll just pass the struct as a output parameter.

    Code:
    void MyData::GetData()
    {
        Fparams fParam;
        fparam = AllData(fParam);
    }
    
    void MyData::AllData( Fparams& fParam)
    {
        fparam.i = 5;
        fparam.flag = true;
    }

  3. #3
    Join Date
    Aug 2012
    Posts
    53

    Re: Returning struct from function

    How can i define a =operator for this struct?

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,477

    Re: Returning struct from function

    Quote Originally Posted by Don Guy View Post
    What's wrong here?
    The whole code snippet you have posted does not make any sense!
    Both MyData methods you showed use local Fparams variables that go out of scope after methods return.
    Quote Originally Posted by Don Guy View Post
    How can i fix this?
    Fix these design bugs.
    Use code tags while posting code snippets. Otherwise your code is unreadable.
    Victor Nijegorodov

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,131

    Re: Returning struct from function

    Quote Originally Posted by VictorN View Post
    The whole code snippet you have posted does not make any sense!
    Both MyData methods you showed use local Fparams variables that go out of scope after methods return.

    Fix these design bugs.
    Use code tags while posting code snippets. Otherwise your code is unreadable.
    That was my thought as well, but I assume he's just posting a simplified example.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,131

    Re: Returning struct from function

    Quote Originally Posted by Don Guy View Post
    How can i define a =operator for this struct?
    Search for C++ overload assignment operator, or something like that. Using an output parameter as I illustrated is simpler and pretty common.

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,477

    Re: Returning struct from function

    Quote Originally Posted by GCDEF View Post
    That was my thought as well, but I assume he's just posting a simplified example.
    Perhaps...
    But posting "a simplified example" that does nothing and is meaningless seems to be disrespect to the Forum, IMHO.
    Victor Nijegorodov

  8. #8
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Returning struct from function

    Quote Originally Posted by Don Guy View Post
    Hello all,

    In my Header file:
    1) Please use code tags when posting code. You should know by now that code posted without code tags winds up looking like a mess.

    2) Post the actual code you're testing. The code you posted can never work, since it could never compile.
    Code:
    class MyData
    {
        struct Fparams
        {
            int i;
            bool flag;
        };
    }   // <-- Missing semicolon
    Code:
    void MyData::GetData()  //???? Where did this function come from??
    //..
    MyData::Fparams MyData::AllData()  // And this one ??
    If you get a compiler error, it is imperative you post the code as-is, directly from the editor. Don't type the code in -- actually copy and paste the code in the forum message. Also, you should post the code that allows us to take the code directly from your message, and without any edits from us, copy it into our compiler window and compile.

    Otherwise, we won't know if your real code has all of these typos and missing entities that you're showing in the message, and we go down the wrong path as to the cause of the compiler error. For example, you have missing semicolons, and those functions could never exist since they are not defined in the MyData class.

    As to your question, a struct can be returned. Now I have no idea what that code you posted is supposed to show, due to the things I pointed out.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 24th, 2013 at 03:33 PM.

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,629

    Re: Returning struct from function

    When you return fparam, you return a copy, and the compiler doesn't
    know how to copy it. You'll need to define an = operator.
    If the user doesn't supply one, doesn't the compiler automatically
    generate a copy assignment operator that does a member by member copy ?
    (Unless there is a user declared move constructor and/or a user declared
    move assignment operator.)

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,994

    Re: Returning struct from function

    Quote Originally Posted by VictorN View Post
    The whole code snippet you have posted does not make any sense!
    Both MyData methods you showed use local Fparams variables that go out of scope after methods return.

    Fix these design bugs.
    There is nothing wrong here since he's returning an object instance, the local objects are copied (hence the need for an operator=). Depending on what's inside this object this may even benefit from an assignment operator with move semantics.

    It would be an error if he returned a pointer of reference to a local object.




    whether it is optimal to return this particular class as an object or whether returning via parameter would be more beneficial is another discussion entirely.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Returning struct from function

    Quote Originally Posted by Philip Nicoletti View Post
    If the user doesn't supply one, doesn't the compiler automatically
    generate a copy assignment operator that does a member by member copy ?
    (Unless there is a user declared move constructor and/or a user declared
    move assignment operator.)
    Yes, that's why a struct instance can be returned in this case, unless that struct has copying turned off by doing the usual things (making copy/assignment private, deriving from a base class that makes the object non-copyable, etc.).

    However, the OP says they get that error, so posting the real code is required.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center