473,396 Members | 1,784 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,396 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 1761
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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.