0

I am getting below error.

TList::operator=':unable to match function definition to an existing declaration.visual studio community 2017. error code: C2244.

The error is at the bottom of this question where I am trying to define the copy assignment operator:

    #include <iostream>
    #include <utility>
    #include "tnode.h"

    // Declaration of class TList

    template <typename T>
    class TList
    {
        friend class TListIterator<T>;


    public:
        TList();        // create empty linked list
        TList(T val, int num);// create list with num copies of val
        ~TList();               // destructor
        TList(const TList& L);      // copy constructor
        TList operator=(const TList& L);// copy assignment operator
        TList(TList && L);      // move constructor
        TList operator=(TList && L);// move assignment operator



    private:
        Node<T>* first;     // pointer to first node in list
        Node<T>* last;      // pointer to last node in list
        int size;           // number of nodes in the list
        static T dummy; // dummy object, for empty list data returns
        //  assuming type T has default construction

    };




    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    template <typename T> 
    TList<T>& TList<T>::operator = (const TList& L)
    {
         size = L.size;
         return *this;
    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    `TList operator=(const TList& L);` etc. TList isn't a full type; TList is – UKMonkey Mar 19 '18 at 12:02
  • 1
    @UKMonkey it is within the class definition, however outside it isn't. With trailing return it is even weirder. `auto TList::operator=(...) -> TList&` *is valid* – Caleth Mar 19 '18 at 12:41

1 Answers1

2

Firstly, you've declared the function as

TList operator=(const TList& L);

yet you define it as

TList<T>& TList<T>::operator = (const TList& L)

Notice the return type is not the same (the definition returns a reference).

Secondly, you are probably trying to define your template functions in a separate source file. See this question.

DeiDei
  • 10,205
  • 6
  • 55
  • 80