CursorCC
CursorCC

Reputation: 23

How can I define the function used in a function template?

I started to study template in C++ today and I've tried to write a simple code. Then I want to write another function(display) inside the original function(function) using the template parameter(a), but I couldn't find a way to define the "display" function correctly. Is there any solution to pass the compile? Or should I use class template(which I haven't studied yet, but if it's needed, I'll read related materials at once)? By the way, my mother tongue is not English so I used a little translator. Some of my description might seem weird and I'm sorry about that.

I learned that template has its variable scope so I tried to add{}, but it doesn't work. What's more, I don't want to copy the code into the "function" so I have no idea what to do.

template <typename T>
void function(T a[],int n)
{
    cout<< "now you are in function." <<endl;
    for(int i = 0; i < n; i++)
    {
        display(a,i);//Here I have to use "a"
        cout << a[i] << " ";
    }
    cout << endl;
}

void display(T a[],int n)
{
    cout << "now you are in display." << endl;
    for(int i = 0; i < n; i++)
    {
        cout << a[n-i] << " ";
    }
    cout << endl;
}

Here's what compiler says: error: variable or field ‘display’ declared void void display(T a[],int n) error: ‘T’ was not declared in this scope But I can't use another typename before the "display" function.

Upvotes: 0

Views: 74

Answers (1)

Sophie
Sophie

Reputation: 434

There are two problems with your code:

  1. display is defined after function, and therefore cannot be used within function; and
  2. display should be a template function in order to use the typename T.

Since display is defined after function, the compiler will not be able to find display when you try to call it within function. You can either declare display before defining function, which basically tells the compiler that the function is defined somewhere else, or you can just move the definition of display above `function.

In addition, you need to make display a template as well so that it can use the type T.

You can fix both problems the easiest way by doing this:

template <typename T>
void display(T a[],int n)
{
    // your code here
}

template<typename T>
void function(T a[],int n)
{
    // your code here
}

If you really wanted the definition for display to come after that of function, you could declare display before function is defined:

template<typename T>
void display(T a[], int n);

template<typename T>
void function(T a[], int n)
{
    // your code here
}

template<typename T>
void display(T a[], int n)
{
    // your code here
}

Edit: After you update display to be a template, you'll need to update your code to call it accordingly:

template<typename T>
void function(T a[], int n)
{
    // ... beginning of the function ...
    for (int i = 0; i < n; i++)
    {
        display<T>(a, i); // Note the addition of the template parameter
        cout << a[i] << " ";
    }
    // ... rest of the function ...
}

Upvotes: 1

Related Questions