这是对此的跟进问题:error C2504 circular inclusion。 如果在父标题中声明了前进的子项。
它不包含在内,所以编译器不会找到Child?然后我如何从Parent对象中实例化一个新的Child对象。
Parent.h
#pragma once
#include <vector>
using std::vector;
class Child;
class Parent
{
public:
Parent();
void GiveBirth();
~Parent();
vector<Child*> children;
};
Parent.cpp
#include "stdafx.h"
#include "Parent.h"
Parent::Parent()
{
}
void Parent::GiveBirth()
{
Child ch = Child(); //Error: incomplete type is not allowed
}
Parent::~Parent()
{
}
Child.h
#pragma once
#include "Parent.h"
class Child : Parent
{
public:
Child();
~Child();
};
Child.cpp
#include "stdafx.h"
#include "Child.h"
Child::Child()
{
}
Child::~Child()
{
}
答案 0 :(得分:2)
以下是一些阅读清单。
http://en.wikipedia.org/wiki/Opaque_pointer
http://en.wikibooks.org/wiki/C%2B%2B_Programming/Idioms#Pointer_To_Implementation_.28pImpl.29
<强> Parent.cpp 强>
#include "stdafx.h"
#include "Parent.h"
#include "Child.h"
Parent::Parent()
{
}
void Parent::GiveBirth()
{
//Child ch = Child(); //Error: incomplete type is not allowed
children.push_back(new Child());
}
Parent::~Parent()
{
}