Thread: Problem with nested classes

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Problem with nested classes

    Hi,

    I have a little problem with nested classes :

    The code is:

    Code:
    #include<iostream.h>
    
    class outside 
    {
      public:
        int age;
    
    public:
    
        outside( int age )
        {
            this->age = age;
        } 
        void show_outside() {
            cout<<"Age="<<this->age;
        }
    
    class inside : public outside 
    {
       int salary;
       public:
       
       inside( int salary)
       {
           this->salary = salary; 
       }
    
       void show_inside() {
            cout<< "Salary="<<this->salary<<this->age;  
       }
    };
    
    };
    
    int main()
    {
        outside a( 45 ) ;
        outside::inside b( 23 ) ;
       
        a.show_outside();
        b.show_inside(); 
    }
    I am getting the following errors:
    nestedclasses.cpp:19: error: invalid use of incomplete type ‘class outside’
    nestedclasses.cpp:4: error: forward declaration of ‘class outside’
    nestedclasses.cpp: In member function ‘void outside::inside::show_inside()’:
    nestedclasses.cpp:29: error: ‘class outside::inside’ has no member named ‘age’

    I think the problem is the way I am inheriting the class outside.....
    Can any body suggest me the sol. ??

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot inherit from outside from inside because outside is being defined. You must remove the nesting by moving inside outside the outside class and then inheriting from it.
    And please, DO indent correctly:
    Code:
    class outside 
    {
    public:
    	int age;
    
    public:
    
    	outside( int age )
    	{
    		this->age = age;
    	} 
    	void show_outside() {
    		cout<<"Age="<<this->age;
    	}
    
    	class inside : public outside 
    	{
    		int salary;
    	public:
    
    		inside( int salary)
    		{
    			this->salary = salary; 
    		}
    
    		void show_inside() {
    			cout<< "Salary="<<this->salary<<this->age;  
    		}
    	};
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Thanks for suggestion.......

    Since if can't use inheritance then what are the benefits of using nested classes ?

    Thanks

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can use inheritance, but not to the class that you are currently defining.
    Nested classes are good if the class will only be used inside the class it's being defined in.
    It also allows you to not pollute the namespace. This allows several classes to have a subclass called CUtility or the like, and customize it to their liking while not having to be inside a namespace of their own.

    And it's not limited to classes. You can out enums, structs, typedefs, as well.

    Valid:
    Code:
    class A { };
    class B { class C: public A { } };
    Invalid:
    Code:
    class A { class B: public A { } };
    class C { };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bargi View Post
    Thanks for suggestion.......

    Since if can't use inheritance then what are the benefits of using nested classes ?

    Thanks
    Nested classes are useful when a class is used as an integral part of another class (you can essentially have the same class inside several different classes without having different names, for example).

    But if you want to inherit from a class, it usually means that you expect the base class to be used several times to derive from, and that usually also means that you don't actually want to derive from the class itself inside it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Thanks for help !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes problem
    By wabbz111 in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2009, 04:57 AM
  2. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 06:21 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  5. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 03:57 AM