C++ 错误:不匹配调用 '(std::string {aka std::basic_string<char>}) (std::string&)'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17262984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:03:24  来源:igfitidea点击:

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

c++stringconstructorcompiler-errorsinitialization

提问by Dreamer

I'm new to 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. The Course class has 3 fileds:

我是 C++ 的新手。我搜索了很多次,但仍然无法得到答案。我正在编写一个名为 Course 的类来描述学生在学校学习的课程。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?

然后它运作良好。我不明白编译器在抱怨什么以及为什么我不能使用直接初始化?

回答by Andy Prowl

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. If you want to initialize them there, you're late: you've missed the train.

初始化是在构造时赋予对象一个值。当您的setName()成员函数被调用时,调用它的对象(及其数据成员)已经被构造。如果你想在那里初始化它们,你就迟到了:你错过了火车。

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

回答by juanchopanza

You are attempting to assignthe 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 initializeobjects that have already been constructed.

在这种情况下,这在语法上是无效的。您不能初始化已经构造的对象。