【发布时间】:2018-12-24 02:47:36
【问题描述】:
编辑:已解决。我是个白痴。我把“”放在数字 64 周围。
这里有一个与 构造函数 相关的 C++ 问题的初学者程序员。当我编译(通过 Xcode)我得到这个错误:
'Phone'的初始化没有匹配的构造函数
我正在通过本教程自学: https://youtu.be/vLnPwxZdW4Y?t=12340
下面列出的代码,但如果它更易于阅读,我也有一个屏幕截图。 SCREENSHOT INCLUDING ERROR MESSAGES
谢谢。
#include <iostream>
// Creating a Class for the type of phone.
class Phone {
public:
std::string manufacturer;
std::string model;
int capacity; //in GB
// Creating a Constructor. This will be called whenever we create a "Phone" object.
Phone(std::string aManufacturer, std::string aModel, int aCapacity)
{
manufacturer = aManufacturer;
model = aModel;
capacity = aCapacity;
}
};
int main()
{
// Objects
Phone iPhone("Apple", "6s", "64"); // This is where the error occurs
}
【问题讨论】:
-
Phone期望int为aCapacity,但您传递的是"64"。试试Phone iPhone("Apple", "6s", 64); -
是的,这修复了它。现在我觉得自己像个白痴。谢谢@songyuanyao!
标签: c++ constructor int