-
Class Membership Problem
Hello, I'm having some trouble getting a piece of code to work, I've simplified it down as much as I could... so here it is.
It is basically an organization issue because I want to add a few more functions to a class which are unrelated to previously existing functions within the class(e.g. I want to separate object management functions from file IO functions but still have them in the same class), so I made a master flex.cpp file where the class is defined with all of the prototypes...
typically I build the class with each function defined completely from within, no prototyping, but then as the class grows it begins to get messy, so here is the problem, how would I make a function (who returns a pointer to a structure within the class) a member of a class? For instance:
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
class TEST
{
public:
struct node
{
int data;
};
node* f(void);
};
node* TEST::f(void)
{
node *ptr;
return (ptr);
}
int main(void)
{
cout << "blah... not needed" << endl;
return 0;
}
Here are the errors I receive: [I am using VC++ 2005 Express Edition]
Code:
1>.\class.cpp(16) : error C2143: syntax error : missing ';' before '*'
1>.\class.cpp(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\class.cpp(17) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\class.cpp(17) : error C2556: 'int *TEST::f(void)' : overloaded function differs only by return type from 'TEST::node *TEST::f(void)'
1> .\class.cpp(13) : see declaration of 'TEST::f'
1>.\class.cpp(17) : error C2371: 'TEST::f' : redefinition; different basic types
1> .\class.cpp(13) : see declaration of 'TEST::f'
I would appreciate any help with this... I have been stuck on it for about 2 days and I cannot proceed any further without fixing this issue, Thanks in advance for any help!
-
Code:
class foo
{
int bar();
};
int foo::bar()
{
return 0;
}
EDIT: Actually, after a second look at your code, you've done this correctly. The problem is probably that struct you have in your class. You might just want to get rid of that.
-
I am afraid it is not that simple UMR_Student... I am trying to return a pointer to the structure... not a simple integer, thank you though.
-
Gotcha, but is there any reason in particular why the struct has to be defined inside the class?
-
Hey... that's an interesting question... I tried moving just before the class, and it got rid of all errors in the test code, I will try to implement this in my larger setup and get back to you all...
Thanks man, that suggestion really helped! Why did it have to be so simple? lol..
-
The correct way to do this is:
Code:
TEST::node* TEST::f(void)
{
}
The reason you have to do this is because the compiler doesn't know that f is a member of test when it parses the return type.
Edit:
A parameter list of "void" is deprecated in C++. You really shouldn't do that.