This is a problem I have encountered when inheriting a virtual base class without having default arguments.
X (base class)
Y( virtual base class, derived from X)
A(derived class, derived from Y)
I have made provision in class Y to pass values to the constructor of X but when I try to create an object of class A, it get the following error message
Error Message:
---------------------
Compiling...
Cpp2.cpp
D:\My Projects\SampleProj\Cpp2.cpp(31) : error C2512: 'X::X' : no appropriate default constructor available
Error executing cl.exe.
--------------------
Code:
----------------------------
#include <iostream>
using namespace std;
class X
{
int i;
public:
X(int p){ i = p; }
void show() { cout << "show:the value of i is " << i << endl; }
};
class Y : virtual public X
{
public:
Y(int y) : X(y)
{
cout << "In y" << endl;
}
};
class A: public Y
{
public:
A(): Y(10)
{
cout << "In A" << endl;
}
};
int main()
{
A a1;
a1.show();
return 0;
}
------------------------------
Is there any specific reason for this behaviour. If you can answer this question, would be of great help.
Regards
Pankajdynamic