重载的父类构造函数。接种剂选择错误? [英] Overloaded parent class constructors. Wrong inizializer choice?

查看:131
本文介绍了重载的父类构造函数。接种剂选择错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Child类添加到已经定义并声明了Parent的现有项目中。父类有两个带有initializer-list的构造函数。这是我的代码,它会生成错误C2668:'Parent :: Parent':对重载函数的歧义调用。我的错误在哪里?

I would like to add a Child class to a pre-existing project having Parent already defined and declared. Parent class has got two constructors with initializer-list. This is my code, and it generates error C2668: 'Parent::Parent' : ambiguous call to overloaded function. Where is my error?

感谢@Mape提供您的片段

Thanks @Mape for your snippet

#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 类构造函数使用一个参数调用时,实际上具有相同的函数签名。

Compiler doesn't know which constructor you intend to call. For example, your Parentclass constructors, when called with one parameter, really have the same function signature.

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吗?

Is this suppose to print Foo or Bar?

使其正常工作


  • 更改代码,使 myInt 参数不是可选的。

  • Change the code so that myInt parameter is not optional.

OR


  • 删除另一个仅使用 myDouble 作为参数的构造函数。 / li>
  • Remove the other constructor which only takes myDouble as parameter.

这篇关于重载的父类构造函数。接种剂选择错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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