误差C2504圆形包含 [英] error C2504 circular inclusion

查看:279
本文介绍了误差C2504圆形包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有父类和子类。子代从父代继承。我想把一个父对象的子对象存储在一个Child对象的向量中。



我将Child标头包含在父标头中,但是我必须包含父标头



如何克服这个循环包含?



Parent.h



  #pragma once 
#include< vector&
#includeChild.h

使用std :: vector;
class Parent
{
public:
Parent();
〜Parent();
vector< Child>儿童;
};

Parent.cpp

  #includestdafx.h
#includeParent.h


Parent :: Parent b $ b {
}


Parent ::〜Parent()
{
}

Child.h

  #pragma once 
#includeParent.h
class Child:Parent
{
public:
Child
〜Child();
};

Child.cpp

  #includestdafx.h
#includeChild.h


Child :: Child b $ b {
}


Child ::〜Child()
{
}

错误



child.h(4):错误C2504: 'parent':base class undefined



parent.h(11):error C2065:'Child':undeclared identifier



Parent.h

  #pragma once 
#include< vector>

使用std :: vector;
class Child;
class Parent
{
public:
Parent();
〜Parent();
vector< Child *>儿童;
};


i have Parent and Child classes. Child inherits from Parent. I want to store in a vector of Child objects the children of a parent object.

I include the Child header into the Parent header, but i have to include the Parent header into the Child header (since it inherits from Parent).

How do i overcome this circular inclusion?

Parent.h

#pragma once
#include <vector>
#include "Child.h"

using std::vector;
class Parent
{
public:
    Parent();
    ~Parent();
    vector<Child> children;
};

Parent.cpp

#include "stdafx.h"
#include "Parent.h"


Parent::Parent()
{   
}


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()
{
}

Errors

child.h(4): error C2504: 'Parent' : base class undefined

parent.h(11): error C2065: 'Child' : undeclared identifier

解决方案

Forward declare Child, and store pointer inside of vector.

Parent.h

#pragma once
#include <vector>

using std::vector;
class Child;
class Parent
{
public:
    Parent();
    ~Parent();
    vector<Child*> children;
};

这篇关于误差C2504圆形包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆