function templates

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::printArray(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::printArray<double>(double*, int)'
.../cpp_test.cpp:22: undefined reference to `void
Array::printArray<int>(int*, int)'
.../cpp_test.cpp:23: undefined reference to `void
Array::printArray<char>(char*, int)'

why?
 
I

Ian Collins

josh said:
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
^^^^^^
A friend function isn't a member function. Either drop the friend, or
drop the class prefix in the definition.
 
J

josh

^^^^^^
A friend function isn't a member function. Either drop the friend, or
drop the class prefix in the definition.

sorry the code above is wrong. Take it without friend! so:

class Array
{
public:
Array() {}
~Array() {}

template <typename T>
void printArray(T el[], int dim);

};

and

template <typename T>
void Array::printArray(T el[], int dim)
{
cout << "[ ";
for(int n=0; n < dim; n++)
{
cout << el[n] << " ";
}
cout << "]\n";
}

and my question is: can we write only a funcion template in a non-
template class like Java do?

I'm a Java programmer that wants to pass to C++ and I want to convert
my Java background into C++...
and this is very HARD!
 
I

Ian Collins

*Please* don't quote signatures.
sorry the code above is wrong. Take it without friend! so:
In that case, your compiler probably doesn't support compiling templates
in separate compilation units. Put the template definition in the header.
 
J

josh

*Please* don't quote signatures.


In that case, your compiler probably doesn't support compiling templates
in separate compilation units. Put the template definition in the header.

great it runs! If I want compiling templates in separate compilation
units how do make that? I'm using
gcc 4.1.1 on Linux
 
I

Ian Collins

josh said:
great it runs! If I want compiling templates in separate compilation
units how do make that? I'm using
gcc 4.1.1 on Linux
By not quoting signatures?

You can't, gcc doesn't support the export keyword.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top