1

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);
}
eliaroseX
  • 47
  • 8
  • You can't forward-declare a class member outside of a class; this has nothing to do with templates. Drop the declaration of `operator=`, on a line marked `First error` – Igor Tandetnik Aug 17 '19 at 04:39
  • Not the cause of immediate problem, but likely to come up next: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Igor Tandetnik Aug 17 '19 at 04:41
  • @IgorTandetnik you are right. no more error on this line – eliaroseX Aug 17 '19 at 04:45
  • You can't declare the `operator=` before you define the class. That needs to go below the definition of the class. Also, templates must be defined in the header file, not the cpp. – David G Aug 17 '19 at 04:46
  • @0x499602D2 do you mean I dont need each template lines above the function implementations in the .cpp? – eliaroseX Aug 17 '19 at 04:51
  • @0x499602D2 "Also, templates must be defined in the header file, not the cpp" Huh? – doug Aug 17 '19 at 04:51
  • @eliaroseX No, you do. What I mean is when you are defining the template member functions, those definitions should be in the header file. [This answer explains it better](https://stackoverflow.com/a/495056/701092). – David G Aug 17 '19 at 05:06

1 Answers1

2
template<typename T>
class myArray;

That is not an error. It's a perfectly valid forward declaration of a class template.

template<typename T>
myArray<T>& myArray<T>::operator=(myArray<T>);

That is wrong on couple of accounts.

  1. You may not use objects of type myArray<T> before the class is defined.
  2. operator= may not be declared as a non-member function.

However without forward-declaring this function, multiples errors appears.

Remove the above operator= function declaration, compile your code, and post the error messages. Without that, it's hard to suggest anything useful.

R Sahu
  • 204,454
  • 14
  • 159
  • 270