I want to write a simple program to demonstrate a factory method but it failed to compile. I couldn't figure out why didn't it compile...
here is the compile error:
test.cpp: In member function ‘A* A::create(int, int)’:
test.cpp:10: error: invalid use of incomplete type ‘struct B’
test.cpp:3: error: forward declaration of ‘struct B’
#include<iostream>
usingnamespace std;
class B;
class C;
class A
{
public:
A* create(int a,int b)
{
if (b==1)
{
B* bo = new B;
return bo;
}
else
{
C* co = new C;
return co;
}
}
void setVal(int n)
{
_a = n;
}
protected:
int _a,_b;
private:
};
class B : public A
{
public:
protected:
private:
};
class C : public A
{
public:
protected:
private:
};
int main()
{
}