mslowiak
mslowiak

Reputation: 1838

Using nested class in head class constructor

I want to make something that I pasted in code. I want to use Nested class in Head class, look on code below. What should I do? I was trying to use a nested construktor in initialization list but still not work. Any ideas?

class Head{    
   private:
      int x;
   public:
   Head(int x, const Nested& n){ 
      this->x=x;
   }
   class Nested{
   private:
      int a;
      int b;
   public:
      Nested(int a, int b){
         this->a=a;
         this->b=b;
      }
   }

}

Upvotes: 0

Views: 43

Answers (1)

marcinj
marcinj

Reputation: 50016

You mean you have a compile error? You should define Nested before its use, as below:

class Head{    
   private:
      int x;
   public:

   class Nested {
   private:
      int a;
      int b;
   public:
      Nested(int a, int b){
         this->a=a;
         this->b=b;
      }
   };

   Head(int x, const Nested& n){ 
      this->x=x;
   }
};

int main()
{
   Head::Nested n(0, 0);
   Head h(0, n);
}

Upvotes: 1

Related Questions