Home > OS >  Compile error related to copy assignment operator
Compile error related to copy assignment operator

Time:11-25

Pretty new to c so its probably something simple but the header file I was given declares this method

Course operator=(const Course&);

When I try to write the method header in my .cpp file, which looks like this

Course& Course::operator=(const Course&) {}

I get these errors

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&);

Any advice on how to fix this?

CodePudding user response:

You have a type mismatch between your declaration and implementation. Your declaration returns a Course by value, but your implementation returns a Course& by reference. The latter is the correct thing for an assignment operator to return.

CodePudding user response:

// missing the '&' in the header declaration
Course& operator=(const Course&);
  •  Tags:  
  • c
  • Related