简体   繁体   English

C ++错误:无法调用&#39;(std :: string {aka std :: basic_string <char> })(std :: string&)&#39;

[英]C++ Error: no match for call to ‘(std::string {aka std::basic_string<char>}) (std::string&)’

I'm new to C++. 我是C ++的新手。 I search many times, but still can't get the answer. 我搜索了很多次,但仍然无法得到答案。 I'm writing a class named Course to describe the courses students taking at school. 我正在写一个名为Course的课程来描述学生在学校学习的课程。 The Course class has 3 fileds: Course类有3个文件:

protected:
    string courseName;
    int courseNum;
    float score; 

And I have a public method "setName" to set the course name: 我有一个公共方法“setName”来设置课程名称:

Course &setName(string name)
{
    this->courseName(name);
    return (*this);
}

However, when I tried to compile, the compiler complains that: C++ Error: no match for call to '(std::string {aka std::basic_string}) (std::string&)' I also tried to modify the code to Course &setName(string &name)... And the compiler keeps complaining about the same error. 但是,当我尝试编译时,编译器抱怨: C ++错误:无法调用'(std :: string {aka std :: basic_string})(std :: string&)'我也尝试将代码修改为Course&setName(string&name)...编译器一直抱怨同样的错误。

But if I change the code to: 但是,如果我将代码更改为:

Course &setName(string name)
{
    this->courseName = name;
    return (*this);
}

Then it works well. 然后它运作良好。 I couldn't understand what the compiler is complaining about and why I can't use the direct initialization? 我无法理解编译器在抱怨什么以及为什么我不能使用直接初始化?

I couldn't understand what the compiler is complaining about and why I can't use the direct initialization? 我无法理解编译器在抱怨什么以及为什么我不能使用直接初始化?

Because that's not an initialization. 因为那不是初始化。 That's an assignment. 这是一项任务。 Both assignment an (copy-)initialization make use of the = sign, but don't let that fool you: the two things are fundamentally different. 赋值和(复制)初始化都使用了=符号,但是不要让那个愚弄你:这两件事情根本不同。

Initialization is what gives a value to an object upon construction . 初始化是在构造时为对象赋予值的原因。 When your setName() member function gets called, the object on which it is invoked (as well as its data members) have already been constructed. 调用setName()成员函数时,已经构造了调用它的对象(以及它的数据成员)。 If you want to initialize them there, you're late: you've missed the train. 如果你想在那里初始化它们,你迟到了:你错过了火车。

In a constructor's initialization list , on the other hand, you could initialize your data members as follows: 另一方面,在构造函数的初始化列表中 ,您可以按如下方式初始化数据成员:

Course::Course(std::string name) : courseName(std::move(name)) { }
//                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                                 This would be initialization

You are attempting to assign the value of one string to another, which has already been initialized. 您正在尝试一个字符串的值分配给另一个已经初始化的字符串。 The syntax for that is 语法是

std::string s1 = ....;
std::string s2 = ....;
s2 = s1; // OK, assignment

whereas you are attempting the equivalent of 而你正试图相当于

s2(s1); // error, this is no way to assign s1 to s2

and that is just invalid syntactically in this context. 在这种情况下,这在语法上是无效的。 You cannot initialize objects that have already been constructed. 您无法初始化已构造的对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:不匹配调用 (std::string{aka std::basic_string<char> })(const char[5])&#39; - Error: no match for call to (std::string{aka std::basic_string<char>})(const char[5])' 错误:与“ operator *”不匹配(操作数类型为“ std::string {aka std basic_string <char> }&#39;和{aka std basic_string <char> }&#39;) - error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}') 错误无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char&#39; 赋值 - C++ - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment - C++ 错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;| - error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'| 错误:“ operator ==”不匹配(操作数类型为“ Seat”和“ std :: string {aka std :: basic_string <char> }&#39;) - error: no match for 'operator==' (operand types are 'Seat' and 'std::string {aka std::basic_string<char>}') std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment| 错误:无法匹配'(const std :: basic_string <char>)()' - error: no match for call to '(const std::basic_string<char>) ()' 没有匹配的函数来调用&#39;std :: basic_string <char> :: basic_string C ++ - no matching function for call to 'std::basic_string<char>::basic_string c++ 错误:无法转换&#39;std :: string {aka std :: basic_string <char> 初始化时&#39;&#39;到&#39;char *&#39; - error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in initialization 错误:为&#39;operator std::string {aka std::__cxx11::basic_string 指定的返回类型<char> }&#39; - Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM