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:
protected:
string courseName;
int courseNum;
float score;
And I have a public method "setName" to set the course name:
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.
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?
std::stringdoes not declare this operator which results in the error you are receiving.