Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'list::list' names the constructor, not the type

Tags:

c++

I get this error while compiling my class linked list with constructors. I wanted to do a copy assignment operator, but i get this error 'list::list' names the constructor, not the type. the line is:

list::list& operator= (const list &l)

list is my the name of my class

like image 508
Jack F Avatar asked Oct 25 '12 18:10

Jack F


2 Answers

If you are defining your operator= function inside your class definition, declare it thus:

class list {
  ...
  list& operator=(const list&) { ... return *this; }
};

If you are defining your operator= function outside your class definition, declare it as in this complete and correct example:

class list {
  public:
  list& operator=(const list&);
};
list& list::operator=(const list&) {
  return *this;
}
int main() {}
like image 75
Robᵩ Avatar answered Nov 15 '22 17:11

Robᵩ


This error is pretty self-explanatory.

Use this code:

list& operator= (const list &l)

Outside a class declaration, you have to precise in which scope belongs the function:

list& list::operator= (const list &l)
//    ^^^^^^
like image 28
Synxis Avatar answered Nov 15 '22 16:11

Synxis