原始帖子
编译后,以下代码会产生错误C2436:'__ctor':成员函数或构造函数初始器列表中的嵌套类
in child.h
#include Parent.h
class Child : public Parent
{
public:
Child (List* pList)
: Parent::Parent(pList)
{
}
};
在这里,父级:
in parent.h
class __declspec(dllimport) Parent : public GrandParent
{
public:
Parent (List* pList = NULL);
}
在parent.cpp
中Parent::Parent (List* pList)
:
m_a(1)
,m_b(1)
,GrandParent(pList)
{
}
使孩子班级打电话给父班构造函数是否正确?
ps如果我将儿童构造函数的声明和实施分为.h和.h和.cpp,也会发生同样的情况。我无法更改父类代码,因为它是预编译库的一部分。
在您的建议之后,@mape,@mat,@barry,@praetorian
我意识到问题是由于父类中存在另一个构造函数。多亏了您的建议,我生成了代码,在儿童类中的新帖子管理多个构造函数(带有初始器列表(中的新帖子管理中的错误(最小,完整和可验证(
根据您的示例进行了改编,这会填充罚款。 Parent::Parent
应该只是Parent
。
#define NULL 0
struct List;
class GrandParent
{
public:
GrandParent(List* pList = NULL) {}
};
class Parent : public GrandParent
{
public:
Parent(List* pList = NULL);
int m_a;
int m_b;
};
Parent::Parent(List* pList)
:
m_a(1)
,m_b(1)
,GrandParent(pList)
{
}
class Child : public Parent
{
public:
Child (List* pList)
: Parent(pList)
{
}
};
int main(void)
{
GrandParent grandparent(NULL);
Parent parent(NULL);
Child child(NULL);
return 0;
}