J
josh
Hi if I want only write a template function in a non-template class I
have an error:
in Array.h
class Array
{
public:
Array() {}
~Array() {}
template <typename T> friend
void printArray(T el[], int dim);
};
in Array.cpp
template <typename T>
void Array:
rintArray(T el[], int dim)
{
cout << "[ ";
for(int n=0; n < dim; n++)
{
cout << el[n] << " ";
}
cout << "]\n";
}
then in cpp_test.cpp
Array *a = new Array();
double d[] = {11.1,11.2};
int i[] = {12,13};
char c[] = {'a','b'};
a->printArray(d, 2);
a->printArray(i, 2);
a->printArray(c, 2);
but here the compiler issues this error:
.../cpp_test.cpp:21: undefined reference to `void
Array:
rintArray<double>(double*, int)'
.../cpp_test.cpp:22: undefined reference to `void
Array:
rintArray<int>(int*, int)'
.../cpp_test.cpp:23: undefined reference to `void
Array:
rintArray<char>(char*, int)'
why?
have an error:
in Array.h
class Array
{
public:
Array() {}
~Array() {}
template <typename T> friend
void printArray(T el[], int dim);
};
in Array.cpp
template <typename T>
void Array:
{
cout << "[ ";
for(int n=0; n < dim; n++)
{
cout << el[n] << " ";
}
cout << "]\n";
}
then in cpp_test.cpp
Array *a = new Array();
double d[] = {11.1,11.2};
int i[] = {12,13};
char c[] = {'a','b'};
a->printArray(d, 2);
a->printArray(i, 2);
a->printArray(c, 2);
but here the compiler issues this error:
.../cpp_test.cpp:21: undefined reference to `void
Array:
.../cpp_test.cpp:22: undefined reference to `void
Array:
.../cpp_test.cpp:23: undefined reference to `void
Array:
why?