【问题标题】:No matching constructor for initialization of 'Phone' (CPP)'Phone' (CPP) 的初始化没有匹配的构造函数
【发布时间】: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


【解决方案1】:

您已将“aCapacity”定义为整数。

你需要写:

int main()
{
    Phone iPhone("Apple", "6s", 64);
}

即... 64 不带引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多