c++ - 这是什么错误: "no appropriate default constructor available"?

标签 c++ class constructor

我得到:

main.cpp(13): error C2512: 'myPlayer' : no appropriate default constructor available

这是它引用的代码:

    myPlayer player;

这是构造函数:

myPlayer (int myHealth, int myDamage, int myMoney, int myWeaponID, int myClass) : health(myHealth), damage(myDamage), money(myMoney),
    weapon_id(myWeaponID), p_class(myClass) {}

此外,如何在 Visual Studio 11 上显示每行的编号?

最佳答案

您的构造函数被声明为采用 5 个参数,类型均为 int。您没有提供任何参数。

您的选择是编写不需要参数的构造函数,或者在构造对象时提供参数,例如:myPlayer player(1, 2, 3, 4, 5);

关于c++ - 这是什么错误: "no appropriate default constructor available"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11387926/

相关文章:

c++ - 如果对象没有默认构造函数并且我不允许在其类定义中创建一个对象,我该如何将对象放入节点中?

c++ - 在构造函数之后初始化 C++ const 字段

Java - 强制构造函数不带参数

c++ - 有没有可能的方法来强制使用字符串文字进行隐式构造函数初始化

c++ - 一个 Vector C++ 中的操作

C# - 重新初始化静态类?

scala - 从 Scala 构造函数提前返回

c++ - 如何在 C++ 中捕获运行时错误

c++ - SFML游戏开发书籍——理解FPS计算

Java - 对非静态字段列表进行静态引用