Thread: class declaration in a class, definition outside a class

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    class declaration in a class, definition outside a class

    Hi,
    How can I define a class which I declared in other class? I have such example:
    Code:
    class graph
    {
    private:
            int     **w; // w[i][v] - the weight of the edge from vertex i to v
    public:
            int     vertex_number;
            class bad_size
            {
                    private:
                            int size;
                    public:
                            bad_size(int);
            };
            graph(int);
            int     edge_u_v(int,int);
    };
    And I would like to only declare class bad_size in class graph:
    Code:
    class graph
    {
    private:
            int     **w; // w[i][v] - the weight of the edge from vertex i to v
    public:
            int     vertex_number;
            class bad_size;
            graph(int);
            int     edge_u_v(int,int);
    };
    And define it somewhere else. I tried do this like defining methods:
    Code:
    class graph::bad_size { ... }
    but that's wrong... How can I do that correct?
    Regards.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Are you talking about defining the constructor? You wouldn't use the "class" keyword again. Just imagine how you would define it if it wasn't in another class then treat the container class as a namespace, so you would have:

    Code:
    graph::bad_size::bad_size() {...}
    edit: I just looked at your post again, my answer applies to your first code sample and not necessarily the second. As far as the second goes. I would think that the forward declaration in the class is not sufficient to make it a sub-object of the class. If you want the one class to contain the other, then declare it as in your first example.

    I will try an example though to see if it can work the second way
    Last edited by Darryl; 05-26-2005 at 09:28 AM.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    76
    No, I'm talking about class. I want to declare a class bad_size in the class graph but define it somewhere else (not in the graph class)

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    OK I believe this is the essence of what you want:
    Code:
    class foo
    {
    public:
    	class bar;
    };
    
    class foo::bar
    {
    	int mem;
    public:
    	foo::bar():mem(100){}
    	int GetMem() {return mem;}
    };
    
    #include <iostream>
    int main()
    {
    	foo::bar Abar;
    	std::cout << Abar.GetMem();
    	system("pause");
    }
    which appears to be the same as you did ... this worked for me though

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    76
    Yes. Thanks a lot.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Code:
    foo::bar():mem(100){}        //i think foo is a unwanted name
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by jinhao
    Code:
    foo::bar():mem(100){}        //i think foo is a unwanted name
    How so? or are you saying it's unnecessary? which is true, but it works either way.
    Last edited by Darryl; 05-26-2005 at 10:20 AM.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Quote Originally Posted by Darryl
    How so? or are you saying it's unnecessary? which is true, but it works either way.
    sorry, i made a mistake
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 01:29 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Array of classes within class definition
    By subdene in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2005, 05:57 PM