|
-
February 3rd, 2009, 06:02 AM
#1
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 -
-
February 3rd, 2009, 06:16 AM
#2
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
-
February 3rd, 2009, 06:20 AM
#3
Re: Returning a struct or pointer to struct froma a function
Is this good?
mystruct* getMe() { return (mystruct*) this; }
-
February 3rd, 2009, 06:22 AM
#4
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 -
-
February 3rd, 2009, 06:22 AM
#5
Re: Returning a struct or pointer to struct froma a function
 Originally Posted by streamer
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 ?
-
February 3rd, 2009, 06:26 AM
#6
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;
}
-
February 3rd, 2009, 06:28 AM
#7
Re: Returning a struct or pointer to struct froma a function
 Originally Posted by Skizmo
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 -
-
February 3rd, 2009, 06:53 AM
#8
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;
}
-
February 3rd, 2009, 06:59 AM
#9
Re: Returning a struct or pointer to struct froma a function
 Originally Posted by streamer
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 -
-
February 3rd, 2009, 07:07 AM
#10
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.
-
February 3rd, 2009, 07:14 AM
#11
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 -
-
February 3rd, 2009, 07:28 AM
#12
Re: Returning a struct or pointer to struct froma a function
copy/paste the error and the line of the code where it fails.
-
February 3rd, 2009, 07:38 AM
#13
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 -
-
February 3rd, 2009, 07:47 AM
#14
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;
}
-
February 3rd, 2009, 10:03 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|