对c++来说很新,所以它可能很简单,但给我的头文件声明了这个方法
Course operator=(const Course&);
当我尝试在.cpp文件中写入方法头时,它看起来像这个
Course& Course::operator=(const Course&) {}
我收到这些错误
course.cpp:29:9: error: no declaration matches ‘Course& Course::operator=(const Course&)’
29 | Course &Course::operator=(const Course&) {
| ^~~~~~
In file included from course.cpp:9:
course.h:12:10: note: candidate is: ‘Course Course::operator=(const Course&)’
12 | Course operator=(const Course&);
关于如何解决这个问题有什么建议吗?
您的声明和实现之间存在类型不匹配。声明通过值返回Course
,但实现通过引用返回Course&
。后者是赋值运算符返回的正确内容。
// missing the '&' in the header declaration
Course& operator=(const Course&);