-1
//In file1.hpp
class A
{
    protected:      
    class B
    {
        public:
        B(int a, int b)
        : c(a),
          d(b) {}
        private:
          int c;
          int d;
    };
};

// In file2.hpp

Class C
{
    public:
    class D : public A::B --------- This line shows the error that "no matching function for call to B"
    {
        public:
        D() {};
        virtual ~D() {};
        //something
    }
    class E : public D
    {
        E() {};
        virtual ~E() {};
    }
}

I am getting an error while inheriting the A::B class in D class. In my main.cpp, I instantiate the D class directly. I am assuming that when D class is instantiated, class A::B gets instantiated automatically.

The error is because I am instantiating D class but not giving the parameters for the instantiation of A::B class.

Right now in main.cpp file, I just write C::D obj;

My question is, how do I instantiate from main.cpp, the class D and also give parameters for class A::B? I do not want to pass as parameters while instantiating class D. Is there a way to create a method? In short I can create a method in main.cpp to pass parameters for class A::B but not pass parameters like C::D obj(hi, hey)

Rushikesh Talokar
  • 1,515
  • 4
  • 16
  • 32
varconst
  • 15
  • 6

1 Answers1

0

First you need to declare "class B" as public and you also need to call calls B constructor from class D (or add defalut constructor)

Sample code:

//In file1.hpp
class A
{
    public:      
    class B
    {
        public:
        //B(){}
        B(int a, int b)
        : c(a),
          d(b) {}
        private:
          int c;
          int d;
    };
};

// In file2.hpp

class C
{
    public:
    class D : public A::B //--------- This line shows the error that "no matching function for call to B"
    {
        public:
        D(): B(1,2)
        {};
        virtual ~D() {};
        //something
    };
    class E : public D
    {
        E() {};
        virtual ~E() {};
    };
};

int main()
{
    C c;    
    return 0;
}
Rushikesh Talokar
  • 1,515
  • 4
  • 16
  • 32