Thread: Class Membership Problem

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    15

    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!

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    88
    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.
    Last edited by UMR_Student; 05-27-2007 at 01:26 PM.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    15
    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.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    88
    Gotcha, but is there any reason in particular why the struct has to be defined inside the class?

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    15
    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..
    Last edited by josephjah; 05-27-2007 at 01:49 PM.

  6. #6
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 04:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM