Thread: Error cause by inheritance

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    Error cause by inheritance

    I tried the following code but i couldnt figure out why does the error occurs. Can someone let me know whats wrong with this code ??

    Code:
    class Expression {
     public:
     	Expression() { }
    	virtual ~Expression() { }
    };
    
    
    class aaa : public Expression{
      public:
    //test3.cpp:18: error: syntax error before `*' token
    //test3.cpp:18: error: missing ';' before right brace
    	  aaa(bbb *x,Expression *y){ }
    	  
      private:
    //test3.cpp:20: error: semicolon missing after declaration of `aaa'
                     bbb *lh;
    	 Expression *rh;
    };
    //test3.cpp:23: error: syntax error before `}' token
    class bbb : public Expression{
      public:
    	  bbb(){ }
    
    
    };

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    its because aaa has no clue what bbb does so your creating a pointer of a class which it has no clue of
    Woop?

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    41
    But i thought i can create a bbb object and pass it to the aaa constructor ???

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Change the order of declaration:
    Code:
    class Expression {
     public:
     	Expression() { }
    	virtual ~Expression() { }
    };
    
    class bbb : public Expression{
      public:
    	  bbb(){ }
    };
    
    class aaa : public Expression{
      public:
            aaa(bbb *x,Expression *y){ }
    	  
      private:
             bbb *lh;
             Expression *rh;
    };
    Or add a forward declaration:
    Code:
    class Expression {
     public:
     	Expression() { }
    	virtual ~Expression() { }
    };
    
    class bbb;
    
    class aaa : public Expression{
      public:
            aaa(bbb *x,Expression *y){ }
    	  
      private:
             bbb *lh;
             Expression *rh;
    };
    
    class bbb : public Expression{
      public:
    	  bbb(){ }
    };
    Either solution allows the compiler to know what bbb is when it is compiling aaa.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM