Defining a member function of a template class

I am working with this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
 
template<typename T, int size=0>
class ArrayClass
{
private:
        T array[size];
public:
        T& operator [](int index);
};
 
template<typename T>
T& ArrayClass<T>::operator [](int index)
{
        return array[index];
}
 
int main()
{
        ArrayClass<int> arrayVariable;
        arrayVariable[3] = 5;
        cout << arrayVariable[3] << "\n";
 
        return 0;
}


and getting this error:
invalid use of incomplete type 'class ArrayClass<T, 0>'
declaration of 'class ArrayClass<T, 0>'


Does anyone know what's causing this problem? If I define the function within the class itself, it works fine, but defining it outside gives me these errors.
Last edited on
template<typename T, int size=0> class ArrayClass; Two (count them) template parameters
1
2
template<typename T> //Just one
T& ArrayClass<T>::operator [](int index)


Edit: ¿zero-size arrays?
Last edited on
Still gives me the error if I do:

1
2
3
4
5
template<typename T, int size>
T& ArrayClass<T>::operator [](int index)
{
        return array[index];
}


Edit: I changed the default parameter to 5.
Last edited on
1
2
3
4
5
6
7
template<typename T, int size> //two
T& 
ArrayClass<T>:: //one
operator [](int index)
{
        return array[index];
}
Thanks.
Topic archived. No new replies allowed.