重载父 class 构造函数。错误的初始化器选择?
Overloaded parent class constructors. Wrong inizializer choice?
我想添加一个 Child class 到一个已经定义和声明了 Parent 的现有项目。 Parent class 有两个带有初始化列表的构造函数。这是我的代码,它会生成错误 C2668:'Parent::Parent':对重载函数的调用不明确。我的错误在哪里?
感谢@Mape
#include <stdio.h>
class Parent
{
public:
// Here a constructor with one default trailing argument, i.e. myInt,
// myDouble is not initialised. This is correct, as once one argument
// is initialised, the rule is that all the following ones have to be
// initialised, but the previous ones may not be.
Parent(double myDouble, int myInt=5);
// Here a defauls contructor, as all its arguments (one indeed) are
// initialised
Parent(double myDouble=0);
double m_dVar;
int m_iVar;
};
class Child : public Parent
{
public:
// derived class has to redefine the constructors.
// Q1: All of them?
// Q2: With the same default arguments?
Child(double myDouble, int myInt=0);
Child(double myDouble=0);
};
class User
{
public:
User();
Parent* m_pGuy;
Child* m_pSonOfGuy;
};
// Base Class Implementation
Parent::Parent(double myDouble, int myInt)
{
m_dVar=myDouble;
m_iVar=myInt;
}
Parent::Parent(double myDouble)
{
m_dVar=myDouble;
m_iVar=3;
}
// Derived Class Implementation
// the two arguments contructor is easily implemented.
Child::Child(double myDouble, int myInt)
: Parent(myDouble*2, myInt+1)
{
}
// the one argument contructor may trigger ERROR:
// "ambiguous call to overloaded function."
// (C2668 in Microsoft Visual Studio compiler)
Child::Child(double myDouble)
//: Parent(0) ERROR
: Parent() //OK
{
m_iVar=9;
}
User::User()
: m_pGuy(0)
{
}
// main{
int main(void)
{
User user1();
//Parent parent;
Child child1(8.3,1);
printf("\n\nChild m_dVar %f",child1.m_dVar);
printf("\nChild m_iVar %d",child1.m_iVar);
return 0;
}
编译器不知道您打算调用哪个构造函数。
例如,您的 Parent
class 构造函数在使用一个参数调用时实际上具有相同的函数签名。
Parent::Parent(double myDouble, int myInt=0) {
cout << "Foo";
}
Parent::Parent(double myDouble) {
cout << "Bar";
}
// later
Parent *parent = new Parent(1.2345); // Compiler: which constructor to call?
// does this mean Parent(1.2345, 0)
// or Parent(1.2345)
这是要打印 Foo 还是 Bar?
让它发挥作用
- 更改代码,使
myInt
参数不是可选的。
或
- 删除另一个只接受
myDouble
作为参数的构造函数。
我想添加一个 Child class 到一个已经定义和声明了 Parent 的现有项目。 Parent class 有两个带有初始化列表的构造函数。这是我的代码,它会生成错误 C2668:'Parent::Parent':对重载函数的调用不明确。我的错误在哪里?
感谢@Mape
#include <stdio.h>
class Parent
{
public:
// Here a constructor with one default trailing argument, i.e. myInt,
// myDouble is not initialised. This is correct, as once one argument
// is initialised, the rule is that all the following ones have to be
// initialised, but the previous ones may not be.
Parent(double myDouble, int myInt=5);
// Here a defauls contructor, as all its arguments (one indeed) are
// initialised
Parent(double myDouble=0);
double m_dVar;
int m_iVar;
};
class Child : public Parent
{
public:
// derived class has to redefine the constructors.
// Q1: All of them?
// Q2: With the same default arguments?
Child(double myDouble, int myInt=0);
Child(double myDouble=0);
};
class User
{
public:
User();
Parent* m_pGuy;
Child* m_pSonOfGuy;
};
// Base Class Implementation
Parent::Parent(double myDouble, int myInt)
{
m_dVar=myDouble;
m_iVar=myInt;
}
Parent::Parent(double myDouble)
{
m_dVar=myDouble;
m_iVar=3;
}
// Derived Class Implementation
// the two arguments contructor is easily implemented.
Child::Child(double myDouble, int myInt)
: Parent(myDouble*2, myInt+1)
{
}
// the one argument contructor may trigger ERROR:
// "ambiguous call to overloaded function."
// (C2668 in Microsoft Visual Studio compiler)
Child::Child(double myDouble)
//: Parent(0) ERROR
: Parent() //OK
{
m_iVar=9;
}
User::User()
: m_pGuy(0)
{
}
// main{
int main(void)
{
User user1();
//Parent parent;
Child child1(8.3,1);
printf("\n\nChild m_dVar %f",child1.m_dVar);
printf("\nChild m_iVar %d",child1.m_iVar);
return 0;
}
编译器不知道您打算调用哪个构造函数。
例如,您的 Parent
class 构造函数在使用一个参数调用时实际上具有相同的函数签名。
Parent::Parent(double myDouble, int myInt=0) {
cout << "Foo";
}
Parent::Parent(double myDouble) {
cout << "Bar";
}
// later
Parent *parent = new Parent(1.2345); // Compiler: which constructor to call?
// does this mean Parent(1.2345, 0)
// or Parent(1.2345)
这是要打印 Foo 还是 Bar?
让它发挥作用
- 更改代码,使
myInt
参数不是可选的。
或
- 删除另一个只接受
myDouble
作为参数的构造函数。