c – 为什么我不能使用此构造函数参数构造此用户定义类型的对象
发布时间:2020-12-16 10:41:30 所属栏目:百科 来源:网络整理
导读:我在创建以下类型的对象时遇到问题: struct wordType { string word = ""; int count = 0;};wordType object("hello world"); 我得到的错误是: [Error] no matching function for call to 'wordType::wordType(std::string) 解决方法 您正在尝试使用wordTy
我在创建以下类型的对象时遇到问题:
struct wordType { string word = ""; int count = 0; }; wordType object("hello world"); 我得到的错误是: [Error] no matching function for call to 'wordType::wordType(std::string&) 解决方法
您正在尝试使用wordType没有的构造函数构造wordType对象.您可以将该构造函数添加到您的代码中:
struct wordType { string word = ""; int count = 0; wordType() = default; wordType(const wordType&) = default; wordType(const string &aword) : word(aword) {} // <-- here }; wordType object("hello world"); 或者,您可以在没有任何构造函数参数的情况下使用局部变量,然后将其填充: struct wordType { string word = ""; int count = 0; }; wordType object; object.word = "hello world"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |