Trying to overload the = operator to do a deep copy

I am currently having some issues with overloading the '=' operator to perform a deep copy of a linked list in a templated class. For whatever reason, the source code refuses to compile with that function in there,

In general terms, this is what the function in question looks like:

header file
linkedlist.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

template <class T>
class linkedList
{

  //Assignment operator does a deep copy
  linkedList& operator=(const linkedList &list);

};

#include "linkedList.cpp"
#endif



Implementation file
linkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef LINKEDLIST_CPP
#define LINKEDLIST_CPP

#include "linkedList.h"

//Assignment operator does a deep copy
template <class T>
linkedList<T>& linkedList<T>::linkedList operator =(const linkedList &list)
{
}

#endif 


Here are the errors that visual studio is generating:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Error	1	
error C2143: syntax error : missing ';' before '='     linkedlist.cpp	44	linkedList

Error	2	
error C2350: 'linkedList<T>::{ctor}' is not a static member	linkedlist.cpp	44	linkedList

Error	3	
fatal error C1903: unable to recover from previous error(s); stopping compilation	linkedlist.cpp	44	linkedList

Error	4	
error C2143: syntax error : missing ';' before '='	linkedlist.cpp	44	linkedList

Error	5	
error C2350: 'linkedList<T>::{ctor}' is not a static member	linkedlist.cpp	44	linkedList

Error	6	
fatal error C1903: unable to recover from previous error(s); stopping compilation	linkedlist.cpp	44	linkedList

Last edited on
If I had to guess I would say it's because of the header guards you put in you cpp file. Try removing them and see what happens.
First, do not include .cpp files. Include header files in your source files and compile the source files. Header files will have inclusion guards but not source files.

Template definitions should all be in the header file according to the inclusion model. Move the definition of operator=() into the header file.

Additionally, the return value of operator=() is a dependent type. You may have to add typename before it.
Last edited on
Thank you for the quickly reply. Unfortunately, after removing the header guards, it still generates the same errors.
I just noticed this, too:
linkedList& operator=(const linkedList &list);

linkedList is a class template. Try linkedList<T>.

Hope this helps.
Also, do not name your parameter "list" if you are going to use the entire
std namespace.
Topic archived. No new replies allowed.