I get two errors from the below code and don't know how to solve them:
First error: invalid use of incomplete type 'class myArray<T>'
Second error: declaration of 'class myArray<T>'
I think it is because I am forward-declaring a member function that is not yet implemented on the first call of the class. However without forward-declaring this function, multiples errors appears.
//myArray.h
#ifndef ARRAY_H_INCLUDED
#define ARRAY_H_INCLUDED
#include <iostream>
template<typename T>
class myArray; // Second error
template<typename T>
myArray<T>& myArray<T>::operator=(myArray<T>); // First error
template<typename T>
void swap(myArray<T>&, myArray<T>&);
template<typename T>
class myArray{
T* m_ptr{nullptr};
int m_size{0};
public:
myArray();
explicit myArray(int);
myArray(const myArray&);
~myArray();
myArray& operator=(myArray);
friend void swap<T>(myArray&, myArray&);
};
#endif // ARRAY_H_INCLUDED
//myArray.cpp
#include "array.h"
template<typename T>
myArray<T>::myArray() = default;
template<typename T>
myArray<T>::myArray(int s){
if(s>0){
m_ptr = new T[s]{};
m_size = s;
}
}
template<typename T>
myArray<T>::~myArray(){
delete[] m_ptr;
}
template<typename T>
myArray<T>& myArray<T>::operator=(myArray<T> a){
swap(*this, a);
return *this;
}
template<typename T>
void swap(myArray<T>& a, myArray<T>& b){
std::swap(a.m_ptr, b.m_ptr);
std::swap(a.m_size, b.m_size);
}