CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Returning a struct or pointer to struct froma a function

    In the .h file of my class I defined a structure.

    Now, in the .cpp of the same class, I need to return from a member function a pointer to the structure, say structure* type.

    Anyway, compiler doesn't allow, saying missing storage-class identifier, as if the structure has not been defined. But I did, in the header file, so this can't be.

    Any clues on why I get this problem? Isn't it possible to return a struct pointer, or a struct, from a function?
    How solve this, besides defining the function as void*?

    Thanks in advance and sorry for the bothering.
    - Buzzyous -

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Returning a struct or pointer to struct froma a function

    Yes, it is possible to return a struct pointer, or a struct from a function.
    Show your code.
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2009
    Posts
    42

    Re: Returning a struct or pointer to struct froma a function

    Is this good?

    mystruct* getMe() { return (mystruct*) this; }

  4. #4
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: Returning a struct or pointer to struct froma a function

    Code:
    //Header file: MyClass.h
    
    #include "OtherMyClass.h"
    
    //Structure definition
    	struct choice
    	{
    		OtherMyClass* chTxt;
    		choice* next;
    		CString identifier;
    	};
    
    //Function declaration
    	struct choice* GetChoiceFromID(CString id);
    
    ///////////////////////////////////////////////////////////////////////////
    
    //Implementation file: MyClass.cpp
    
    //Function definition
    struct choice* MyClass::GetChoice(CString id)
    {
    	return pointerToChoice;
    }
    Ops, writing this I understood... Sorry. The structure has class scope, that's why it can't be returned by a function unless it is an internal call.

    Really sorry.
    Last edited by Buzzyous; February 3rd, 2009 at 06:25 AM.
    - Buzzyous -

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Returning a struct or pointer to struct froma a function

    Quote Originally Posted by streamer View Post
    Is this good?

    mystruct* getMe() { return (mystruct*) this; }
    Why should you want to create a function inside a struct to get the pointer of the struct itself ?

  6. #6
    Join Date
    Feb 2009
    Posts
    42

    Re: Returning a struct or pointer to struct froma a function

    //Function definition
    struct choice* MyClass::GetChoice(CString id)
    {
    return pointerToChoice;
    }

    should be

    //Function definition
    choice* MyClass::GetChoice(CString id)
    {
    return pointerToChoice;
    }

  7. #7
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: Returning a struct or pointer to struct froma a function

    Quote Originally Posted by Skizmo View Post
    Why should you want to create a function inside a struct to get the pointer of the struct itself ?
    Sorry, I don't understand what you're saying... The function is not inside the struct; the pointer creates a new struct and returns its pointer.

    Structs have not function inside of them, do they?

    To streamer, it doesn't work anyway, that was my first try...

    The only fact that puzzles me is that the struct is defined inside the class, so it cannot be "exported" outside class scope, but compiler doesn't allow to return a choice* even if the function is declared as protected, so callable only from inside the class...
    Last edited by Buzzyous; February 3rd, 2009 at 06:31 AM.
    - Buzzyous -

  8. #8
    Join Date
    Feb 2009
    Posts
    42

    Re: Returning a struct or pointer to struct froma a function

    Something like this?

    class myClass
    {
    public:
    struct myStruct
    {
    myStruct() { a = 1;}
    int a;
    };

    myStruct ms[10];

    myStruct *getMe(int which) { return &ms[which]; }
    };

    int main(int argc,char **argv)
    {
    myClass mc;
    myClass::myStruct* ms;
    ms = mc.getMe(1);
    printf("%d", ms->a );


    return 0;
    }

  9. #9
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: Returning a struct or pointer to struct froma a function

    Quote Originally Posted by streamer View Post
    Something like this?

    class myClass
    {
    public:
    struct myStruct
    {
    myStruct() { a = 1;}
    int a;
    };

    myStruct ms[10];

    myStruct *getMe(int which) { return &ms[which]; }
    };

    int main(int argc,char **argv)
    {
    myClass mc;
    myClass::myStruct* ms;
    ms = mc.getMe(1);
    printf("%d", ms->a );


    return 0;
    }
    No, the struct has not any functions in it... I told you.

    The structure is defined in the header file, and used in the implementation file of a class. The structure has class scope.
    I need that a member function of the class creates a new structure and return its address as a pointer to struct.

    Did I explained myself better?

    In addition, if I rewrite the struct declaration in the implementation .cpp file also, I don't get the missing storage class error for choice, or myStruct, anymore, but I get error on redefinition of the function...

    I can't understand why this happens; including the headers includes also the struct definition. Why rewrite it in the .cpp file should change anything?
    Last edited by Buzzyous; February 3rd, 2009 at 07:04 AM.
    - Buzzyous -

  10. #10
    Join Date
    Feb 2009
    Posts
    42

    Re: Returning a struct or pointer to struct froma a function

    MY struct also don't have any function in it it is merely inside the scope of the class.

    But writing

    Code:
    struct myStruct
    {
    	int a;
    };
    
    class func
    {
    public:
    	myStruct *getMeStruc()
    	{
    		return new myStruct;
    	}
    };
    is permitted. You have function inside one class that creates new struct and returns pointer to it.

  11. #11
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: Returning a struct or pointer to struct froma a function

    But it doesn't work; it gives error about redefinition of the function getMeStruc().

    I know this is due to the fact that return value is the struct type, because changing the return type the program gets compiled, so it is not a real redefinition problem.

    And in your case, the struct is not declared inside the class... If done so, the program gets compiled, but if I declare the struct inside the class body, it fails... why
    Last edited by Buzzyous; February 3rd, 2009 at 07:26 AM.
    - Buzzyous -

  12. #12
    Join Date
    Feb 2009
    Posts
    42

    Re: Returning a struct or pointer to struct froma a function

    copy/paste the error and the line of the code where it fails.

  13. #13
    Join Date
    Jan 2007
    Location
    Italy
    Posts
    156

    Re: Returning a struct or pointer to struct froma a function

    Code:
    //MyClass.h
    
    #include "MyOtherClass.h"
    
    	struct choice
    	{
    		MyOtherClass* chTxt;
    		choice* next;
    		CString identifier;
    	};
    
    class MyClass : public CWnd
    {
    //class code
    
    protected:
    	choice* GetChoiceFromID(CString id);
    }
    
    
    
    //MyClass.cpp
    
    choice* MyClass::GetChoiceFromID(CString id)
    {
            choice * toReturn;
    //Code to get the choice and store it into toReturn
            return toReturn;
    }
    At the declaration of the function in the cpp file I get the following errors;

    error C2143: syntax error : missing ';' before '*'
    error C2501: 'choice' : missing storage-class or type specifiers
    error C2501: 'GetChoiceFromID' : missing storage-class or type specifiers
    error C2556: 'int *__thiscall MyClass::GetChoiceFromID(class CString)' : overloaded function differs only by return type from 'struct MyClass::choice *__thiscall MyClass::GetChoiceFromID(class CString)'
    error C2371: 'GetChoiceFromID' : redefinition; different basic types
    Last edited by Buzzyous; February 3rd, 2009 at 07:41 AM.
    - Buzzyous -

  14. #14
    Join Date
    Feb 2009
    Posts
    42

    Re: Returning a struct or pointer to struct froma a function

    This probably doesn't have error with code itself, but #ifdef #endif macros.

    If some header is not included, or included once but not from particular file then you will get similar errors.

    Try to move

    choice* MyClass::GetChoiceFromID(CString id)
    {
    choice * toReturn;
    //Code to get the choice and store it into toReturn
    return toReturn;
    }

    to header file inside the class itself, and see what is happening.

    choice* GetChoiceFromID(CString id)
    {
    choice * toReturn;
    //Code to get the choice and store it into toReturn
    return toReturn;
    }

  15. #15
    Join Date
    Nov 2006
    Posts
    14

    Re: Returning a struct or pointer to struct froma a function

    Yo!! Buzz, down here.

    Sorry, but this thread seems to have gone mad. Anyway: You were close, change your code to look like this (my changes in bold):

    Code:
    //Header file: MyClass.h
    
    #include "OtherMyClass.h"
    
    //Structure definition
    	struct choice
    	{
    		OtherMyClass* chTxt;
    		choice* next;
    		CString identifier;
    	};
    
    //Function declaration
    	choice* GetChoiceFromID(CString id);
    
    ///////////////////////////////////////////////////////////////////////////
    
    //Implementation file: MyClass.cpp
    
    //Function definition
    MyClass::choice* MyClass::GetChoiceFromID(CString id)
    {
    	return pointerToChoice;
    }
    You can return a type that has class scope, but in the impl file you need to explicitly give that scope, as above. It also helps a lot if the implementation of a function has the same name as its declaration. (And get rid of those superfluous uses of the word "struct", as I have.)

    HTH.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured