Templates in c ++ can only be used once?

0

I have this code in c ++ and I'm getting an error:

#include<iostream>
using namespace std;
template <class T>

void setVector(T* v, int sizeOfV){
for(int i=0;i<sizeOfV;i++)cin>>v[i];

}
void showVector(T* v, int sizeOfV){
for(int i=0;i<sizeOfV;i++)cout>>endl>>v[i];}

There is the error:

T não está no escopo
V não está no escopo

Can a template only be used once ??? I wanted to clarify this doubt: D

    
asked by anonymous 26.04.2017 / 02:46

1 answer

3

The error occurs because the compiler does not see the type T in the second function. Two functions can not share the same generic type, you need to declare each function as template:

#include<iostream>
using namespace std;

template <class T>
void setVector(T* v, int sizeOfV) 
{
    for (int i = 0; i<sizeOfV; i++)
        cin >> v[i];
}

template <class T>
void showVector(T* v, int sizeOfV)
{
    for (int i = 0; i<sizeOfV; i++)
        cout >> endl >> v[i];
}
    
26.04.2017 / 04:05