如何解决“问题”;没有与参数列表匹配的构造函数实例;C+中的错误+;? 我现在正在学习C++,我正在编写一个利用成员初始化列表的程序。以下是我正在使用的代码: #include <cstdio> struct ClockOfTheLongNow { ClockOfTheLongNow() { year = 2019; } void add_year() { year++; } bool set_year(int new_year) { if (new_year < 2019) return false; year = new_year; return true; } int get_year() const { return year; } private: int year; }; struct Avout{ Avout(const char* name, long year_of_apert) : name{ name }, apert{ year_of_apert } { } void announce() const { printf("My name is %s and my next apert is %d.\n", name, apert.get_year()); } const char* name; ClockOfTheLongNow apert; }; int main() { Avout raz{ "Eramas", 3010 }; Avout jad{ "Jad", 4000 }; raz.announce(); jad.announce(); }

如何解决“问题”;没有与参数列表匹配的构造函数实例;C+中的错误+;? 我现在正在学习C++,我正在编写一个利用成员初始化列表的程序。以下是我正在使用的代码: #include <cstdio> struct ClockOfTheLongNow { ClockOfTheLongNow() { year = 2019; } void add_year() { year++; } bool set_year(int new_year) { if (new_year < 2019) return false; year = new_year; return true; } int get_year() const { return year; } private: int year; }; struct Avout{ Avout(const char* name, long year_of_apert) : name{ name }, apert{ year_of_apert } { } void announce() const { printf("My name is %s and my next apert is %d.\n", name, apert.get_year()); } const char* name; ClockOfTheLongNow apert; }; int main() { Avout raz{ "Eramas", 3010 }; Avout jad{ "Jad", 4000 }; raz.announce(); jad.announce(); },c++,C++,我得到的错误是: no instance of constructor "ClockOfTheLongNow::ClockOfTheLongNow" matches the argument list -- argument types are: (long) 我已经试着寻找解决问题的办法,但到目前为止,还没有成功。预期输出应为: My name is Erasmas and my next apert is 3010. My name is Jad and my next apert is

我得到的错误是:

no instance of constructor "ClockOfTheLongNow::ClockOfTheLongNow" matches the argument list -- argument types are: (long)
我已经试着寻找解决问题的办法,但到目前为止,还没有成功。预期输出应为:

My name is Erasmas and my next apert is 3010.
My name is Jad and my next apert is 4000.

ClockOfLongNow
没有将
long
(或任何其他类型的值)作为输入的构造函数,但您正试图通过将
年份apert
传递给其构造函数来构造
apert
成员

您需要向长时间的时钟添加一个,例如:

struct ClockOfLongNow{

ClockOfLongNow(){/错误告诉您参数中给定的类型没有构造函数

将类型
long
参数赋予
Avout
构造函数,然后在
apert
变量中初始化该构造函数,该变量的类型为
clockOfLongNow

换句话说,您必须为时钟结构创建一个新的构造函数,其外观如下:

ClockOfTheLongNow(long parameterYear) { 
    year = parameterYear;
}

这似乎已经修复了错误。但是,现在我在尝试用g++编译程序时遇到了编译器错误。然后,您的代码中还有其他错误。实际的错误消息是什么?您可能需要就此发布一个新问题。更不用说我自己修复了编译错误。现在一切正常。谢谢谢谢你的帮助。
ClockOfTheLongNow(long parameterYear) { 
    year = parameterYear;
}