473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

function templates

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
5 1759
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Suzanne | last post by:
Hi++, I get a link error when I try to call a template function that is declared in a header file and defined in a non-header file. I do *not* get this link error if I define the template...
3
by: Dennis Pais | last post by:
Here's a piece of code that I found in C++ Primer by Stan Lippman ! ------------------------------------------------- template<typename T, int size> T min(T (&r_array) ) { Type min_value =...
5
by: nifsmith | last post by:
Hi I am trying to learn about Queues and use templates at the same time. I have written the following code and I am getting a link error, stating "unresolved external symbol, "int__cdecl...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
2
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
5
by: desktop | last post by:
I have this example: template<class T(1) void f( T ); template<class T(2) void f( T* ); template< (3)
4
by: nickyspace | last post by:
HI all I have a little issue with this php code. Below is the code CODE: PHP 1.<?
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.