0

I need to use my class template inside one of the member functions (class methods), as shown below :

Declaration :

template <int N>
class Array
{
private :
    int var[N];
public : 
    void increment ();
};

Definition :

template <int N>
void Array<N> :: increment ()
{
    for (int i = 0; i < N; i++)
    {
        cout << i << endl;
    }
}

Instantiation and method call in Main.cpp:

Array <5>var;

int main()
{
    var.increment();

    system ("pause");
}

I keep getting a linking error saying "1 unresolved externals" in Main.obj (Main.cpp). Please consider all "cout" and "using namespace std" to be already done in the background. I'm just adding the relevant code here.

My main problem is that I don't know how to use the class template parameter (N) inside one of the class's methods.

UPDATE: Sorry I'm late but I've found a solution to this. Thanks for the help all.

Tine
  • 55
  • 6

1 Answers1

0

Just to close this thread because I forgot about it long ago. The problem seems to be the fact that templates can only be implemented in a header file of .tpp file. The link given by @user4581301 helped in clearing this up.

This is the link

Tine
  • 55
  • 6