-1

I have a problem with a template class I named "DynamicArray". The only problem that I have is when I define my assignment operator. it gives me two different errors

1) the first error it gives me is

DynamicArray& says "argument list for class template "DynamicArray" is missging"

2) the second error is

DynamicArray::operator= says "template argument list must match the parameter list"

this is my program:

DynamicArray.h

#pragma once
#include <iostream>

using namespace std;

template<typename T>
class DynamicArray
{
public:
    DynamicArray();
    DynamicArray(const DynamicArray &d);
    DynamicArray& operator=(const DynamicArray &d);
    ~DynamicArray();
};

template<typename T>
DynamicArray<T>::DynamicArray()
{

}

template<typename T>
DynamicArray<T>::DynamicArray(const DynamicArray &d)
{

}

template<typename T>
DynamicArray& DynamicArray<T>::operator=(const DynamicArray &d)
{

}

template<typename T>
DynamicArray<T>::~DynamicArray()
{

}

I have looked at multiple examples and can't figure out why I have this problem.

Can anyone tell me what I am doing wrong here?

  • 1
    Apart from anything else: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file –  Feb 06 '19 at 21:57
  • ... and `operator=` should return a `DynamicArray&`. – Ted Lyngmo Feb 06 '19 at 22:00
  • "argument list for class template "DynamicArray" is missing" is quite verbose. You want to return `DynamicArray` by reference, but you didn't specify it's template argument, so compiler doesn't know what exactly do you want to return (in case you wanted to always return `DynamicArray` for some reason...) – Yksisarvinen Feb 06 '19 at 22:00
  • I tried having the definitions in the same header file before moving them to the .cpp file and had the same problem. – Luis Torre Feb 06 '19 at 22:02
  • Only one of them. Fixing the one I mentioned would have removed the other. – Ted Lyngmo Feb 06 '19 at 22:02

1 Answers1

0
  1. Put the complete template in the header file as Neil suggested via the link to Why can templates only be implemented in the header file?.
  2. The return value from operator= should be DynamicArray<T>&
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108