By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,395 Members | 2,537 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,395 IT Pros & Developers. It's quick & easy.

function templates

P: n/a
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 Tfriend
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?

Apr 30 '07 #1
Share this Question
Share on Google+
5 Replies


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

--
Ian Collins.
Apr 30 '07 #2

P: n/a
On 30 Apr, 10:51, Ian Collins <ian-n...@hotmail.comwrote:
josh wrote:
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 Tfriend

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

--
Ian Collins.
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!

Apr 30 '07 #3

P: n/a
josh wrote:
On 30 Apr, 10:51, Ian Collins <ian-n...@hotmail.comwrote:
>josh wrote:
>>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 Tfriend
^^^^^^
A friend function isn't a member function. Either drop the friend, or
drop the class prefix in the definition.
*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.

--
Ian Collins.
Apr 30 '07 #4

P: n/a
On 30 Apr, 11:12, Ian Collins <ian-n...@hotmail.comwrote:
josh wrote:
On 30 Apr, 10:51, Ian Collins <ian-n...@hotmail.comwrote:
josh wrote:
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 Tfriend
^^^^^^
A friend function isn't a member function. Either drop the friend, or
drop the class prefix in the definition.

*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.

--
Ian Collins.
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

Apr 30 '07 #5

P: n/a
josh wrote:
On 30 Apr, 11:12, Ian Collins <ian-n...@hotmail.comwrote:
>josh wrote:
>>On 30 Apr, 10:51, Ian Collins <ian-n...@hotmail.comwrote:
josh wrote:
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 Tfriend
^^^^^^
A friend function isn't a member function. Either drop the friend, or
drop the class prefix in the definition.
*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.

--
Ian Collins.

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.

--
Ian Collins.
Apr 30 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.