473,569 Members | 2,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a template function from another template

I have a template function which print out the content of a list: The
following compiles:
void PrintInt2 (int i) {
cout << i << " ";
}

template <class T>
void printList(T& list) {
// this compiles.
// for_each( list.begin(), list.end(), ptr_fun(&PrintI nt2));
// this does not.
for_each( list.begin(), list.end(), ptr_fun(&Print) );
}

But if i want to change the function PrintInt2 to a template function,
template <class T>
void Print(T item) {
cout << item << " ";
}
it does not compile. Can you please tell me how to make this line
compile?
// this does not.
for_each( list.begin(), list.end(), ptr_fun(&Print) );

Thank you.

Feb 21 '06 #1
8 3046
Pl********@gmai l.com wrote:
I have a template function which print out the content of a list: The
following compiles:
void PrintInt2 (int i) {
cout << i << " ";
}

template <class T>
void printList(T& list) {
// this compiles.
// for_each( list.begin(), list.end(), ptr_fun(&PrintI nt2));
// this does not.
for_each( list.begin(), list.end(), ptr_fun(&Print) );
}

But if i want to change the function PrintInt2 to a template function,
template <class T>
void Print(T item) {
cout << item << " ";
}
it does not compile. Can you please tell me how to make this line
compile?
// this does not.
for_each( list.begin(), list.end(), ptr_fun(&Print) );


I am guessing, but shouldn't it be 'ptr_fun(Print< T>)'? If it doesn't
help, follow the recommendations of FAQ 5.8.

V
--
Please remove capital As from my address when replying by mail
Feb 21 '06 #2
template <typename T>
void Print(T item) {
std::cout << item << " ";
}

template <typename T1,typename T2>
void printList(T1& list) {
for_each( list.begin(), list.end(), &Print<T2>);
}

int main(int argc, char* argv[])
{
std::vector<dou ble> t(10,3.0);
printList<std:: vector<double>, double >(t);
return 0;
}

This is a fast solution!
Print and PrintList doesn't have the same template-prameter!

There are a couple of nice other solution!

Feb 21 '06 #3
Thank you for your suggestion.

I change to this:
template <typename T>
void Print(T item) {
std::cout << item << " ";

}

template <typename T>
void printList(T& list) {
for_each( list.begin(), list.end(), ptr_fun(Print<T >));

}
And in my code, I call pritnList like this:
vector<float> a;
a.push_back(1.2 );
a.push_back(3.2 );
printList(a);

And I have these compile errors:
.../Utils.h:35: error: no match for 'operator<<' in 'std::cout <<
item'
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:67:
note: candidates are: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(std::basi c_ostream<_Char T, _Traits>&
(*)(std::basic_ ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits
= std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:78:
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(std::basi c_ios<_CharT,
_Traits>& (*)(std::basic_ ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:90:
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(std::ios_ base&
(*)(std::ios_ba se&)) [with _CharT = char, _Traits =
std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:125 :
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(long int) [with _CharT
= char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:159 :
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(long unsigned int)
[with _CharT = char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:102 :
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(bool) [with _CharT =
char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:176:
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(short int) [with _CharT
= char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:187:
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(short unsigned int)
[with _CharT = char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:191:
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(int) [with _CharT =
char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:202:
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(unsigned int) [with
_CharT = char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:183 :
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(long long int) [with
_CharT = char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:218 :
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(long long unsigned int)
[with _CharT = char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:242 :
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(double) [with _CharT =
char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:217:
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(float) [with _CharT =
char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:265 :
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(long double) [with
_CharT = char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:288 :
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(const void*) [with
_CharT = char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:311 :
note: std::basic_ostr eam<_CharT, _Traits>&
std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(std::basi c_streambuf<_Ch arT, _Traits>*) [with
_CharT = char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:502:
note: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const unsigned
char*) [with _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:497:
note: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const signed char*)
[with _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:616 :
note: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const char*) [with
_Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:571 :
note: std::basic_ostr eam<_CharT, _Traits>&
std::operator<< (std::basic_ost ream<_CharT, _Traits>&, const char*)
[with _CharT = char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:463:
note: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, unsigned char)
[with _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:458:
note: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, signed char) [with
_Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:509 :
note: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, char) [with _Traits
= std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:447:
note: std::basic_ostr eam<_CharT, _Traits>&
std::operator<< (std::basic_ost ream<_CharT, _Traits>&, char) [with
_CharT = char, _Traits = std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_algo.h:
In function '_Function std::for_each(_ InputIterator, _InputIterator,
_Function) [with _InputIterator = __gnu_cxx::__no rmal_iterator<f loat*,
std::vector<flo at, std::allocator< float> > >, _Function =
std::pointer_to _unary_function <std::vector<fl oat, std::allocator< float>
, void>]':

.../Utils.h:41: instantiated from 'void printList(T&) [with T =
std::vector<flo at, std::allocator< float> >]'
.../BlockList.cpp:4 44: instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_algo.h:158:
error: no match for call to
'(std::pointer_ to_unary_functi on<std::vector< float,
std::allocator< float> >, void>) (float&)'
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_function.h: 493:
note: candidates are: _Result std::pointer_to _unary_function <_Arg,
_Result>::opera tor()(_Arg) const [with _Arg = std::vector<flo at,
std::allocator< float> >, _Result = void]

Feb 21 '06 #4
TB
Pl********@gmai l.com skrev:
Thank you for your suggestion.

I change to this:
template <typename T>
void Print(T item) {
std::cout << item << " ";

}

template <typename T>
void printList(T& list) {
for_each( list.begin(), list.end(), ptr_fun(Print<T >));
When you call printList with a container you need to supply the right
type to Print<>:

for_each( list.begin(), list.end(),
ptr_fun(Print<t ypename T::value_type>) );

}
And in my code, I call pritnList like this:
vector<float> a;
a.push_back(1.2 );
a.push_back(3.2 );
printList(a);

And I have these compile errors:


<autosnipping huge amount of error messages>

--
TB @ SWEDEN
Feb 21 '06 #5
Pl********@gmai l.com wrote:
Thank you for your suggestion.

I change to this:
template <typename T>
void Print(T item) {
std::cout << item << " ";

}

template <typename T>
void printList(T& list) {
for_each( list.begin(), list.end(), ptr_fun(Print<T >));
Since you're calling 'printList' for a 'vector<float>' , your 'T' is
'vector<float>' . If you want to use 'Print<float>', then you need
to replace "Print<T>" with "Print<T::value _type>".

}
And in my code, I call pritnList like this:
vector<float> a;
a.push_back(1.2 );
a.push_back(3.2 );
printList(a);
[..]


V
--
Please remove capital As from my address when replying by mail
Feb 21 '06 #6
Thanks. That works.

I have one related question, how can I pass an argument to this
template:
I change my code to this:
template <class T>
void print2(T item, ostream& os) {
os << item << " ";

}
template <class T>
void printList2(T& list, ostream& os) {
for_each( list.begin(), list.end(), ptr_fun(print2< typename
T::value_type>( os) ));

}

but I have a compile error:
.../Utils.h: In function 'void printList2(T&, std::ostream&) [with T =
std::vector<int , std::allocator< int> >]':
.../XWidthBlockMap. cpp:111: instantiated from here
.../Utils.h:73: error: no matching function for call to
'print2(std::ba sic_ostream<cha r, std::char_trait s<char> >&)'

Feb 22 '06 #7
Pl********@gmai l.com wrote:
I have one related question, how can I pass an argument to this
template:
I change my code to this:
template <class T>
void print2(T item, ostream& os) {
os << item << " ";

}
template <class T>
void printList2(T& list, ostream& os) {
for_each( list.begin(), list.end(), ptr_fun(print2< typename
T::value_type>( os) ));
the last argument in 'for_each' is an attempt to get 'ptr_fun' from
a function call:

ptr_fun( print2<blah>(os ) )

Here, "print2<blah>(o s)" is a function call. You probably want to define
your own functor (since 'bind2nd' isn't going to work) to pass 'os' to the
function _every_ time 'for_each' calls your functor. It would look
something like

struct my_print2 {
ostream& os;
my_print2(ostre am& os) : os(os) {}
template<class T> void operator()(T t) {
print2(t, os);
}
};

and pass an instantiation of that functor to the 'for_each' instead of
'ptr_fun'.

}

but I have a compile error:
../Utils.h: In function 'void printList2(T&, std::ostream&) [with T =
std::vector<int , std::allocator< int> >]':
../XWidthBlockMap. cpp:111: instantiated from here
../Utils.h:73: error: no matching function for call to
'print2(std::ba sic_ostream<cha r, std::char_trait s<char> >&)'


You're trying to call a function that doesn't exist.

V
--
Please remove capital As from my address when replying by mail
Feb 22 '06 #8
TB
Pl********@gmai l.com skrev:
Thanks. That works.

I have one related question, how can I pass an argument to this
template:
I change my code to this:
template <class T>
void print2(T item, ostream& os) {
os << item << " ";

}
It takes two arguments now, not one.
template <class T>
void printList2(T& list, ostream& os) {
for_each( list.begin(), list.end(), ptr_fun(print2< typename
T::value_type>( os) ));

}
That will not compile.

but I have a compile error:
../Utils.h: In function 'void printList2(T&, std::ostream&) [with T =
std::vector<int , std::allocator< int> >]':
../XWidthBlockMap. cpp:111: instantiated from here
../Utils.h:73: error: no matching function for call to
'print2(std::ba sic_ostream<cha r, std::char_trait s<char> >&)'


Learn to read error messages, it's a valuable human trait.

If you really want to learn how this all works, then I recommend
buying a copy of Josuttis' The C++ Standard Library.

Here's one solution:

template<typena me T>
struct print : public std::unary_func tion<T,void> {
void operator()(T item) {
d_os << item << " ";
}
std::ostream & d_os;
print(std::ostr eam & os) : d_os(os) {}
};

template<typena me T>
void printList(T const & list, std::ostream & os) {
std::for_each(l ist.begin(), list.end(), print<T::value_ type>(os));
}

--
TB @ SWEDEN
Feb 22 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2152
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string containting a template for a tuple, the function then finds a tuple that matches this template and then returns it. The template may contain variable...
10
3194
by: jim.brown | last post by:
Please refer me to the right place if this is the wrong place to post this question. I'm looking for an example of calling the Eigenvalue routines of JAMA from a C++ program. The documentation says that the is the public method: Eigenvalue (const TNT::Array2D< Real > &A) I write this program
12
2442
by: johny smith | last post by:
I am trying to figure out a way to print the address of what called a certain function once inside the function. I am assuming that this information is on the stack somewhere. But can someone give me some advice? I don't know where to go to look this information up. Do I need to access the stack, and hence do some kind of inline...
19
4230
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const char* formatter, ...); Then, I want to call it as so:
2
1195
by: RalphTheExpert | last post by:
This thread is a continuation of a thread with the Subject "Unhandled exception - Different under debugger and non-debugger". (http://www.dotnetnewsgroups.com/newsgroupthread.asp?ID=186902) Oleg and Carl: Carl wrote: "I would think the only fully sanitary, guaranteed to work way would be to either terminate the program from inside the...
0
357
by: Plissken.s | last post by:
I have a template function which print out the content of a list: The following compiles: void PrintInt2 (int i) { cout << i << " "; } template <class T> void printList(T& list) {
2
2361
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out exactly what the problem is. Is it related to the fact that a nested class is involved? #include <iostream> using std::cout;
5
1600
by: Jeff Newman | last post by:
Hi all, I am trying to figure out what is causing the compile error in the following example. I have two functions which are identical (except one is templated), both trying to call a template function in another class. The template function (func2) won't compile. I'm hoping that I'm just missing something simple, but I can figure out...
6
11561
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use another convention unless they have an ellipsis argument.) I can force GCC and other compilers to use such a convention by declaring all methods with...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.