473,378 Members | 1,631 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,378 software developers and data experts.

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(&PrintInt2));
// 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 3027
Pl********@gmail.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(&PrintInt2));
// 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<double> 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_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&
(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits
= std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:78:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT,
_Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:90:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&
(*)(std::ios_base&)) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:125:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT
= char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:159:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int)
[with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:102:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:176:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT
= char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:187:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int)
[with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:191:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:202:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:183:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:218:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int)
[with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:242:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:217:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:265:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:288:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:311:
note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:502:
note: std::basic_ostream<char, _Traits>&
std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned
char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:497:
note: std::basic_ostream<char, _Traits>&
std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
[with _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:616:
note: std::basic_ostream<char, _Traits>&
std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with
_Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:571:
note: std::basic_ostream<_CharT, _Traits>&
std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
[with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:463:
note: std::basic_ostream<char, _Traits>&
std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
[with _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:458:
note: std::basic_ostream<char, _Traits>&
std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with
_Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/ostream.tcc:509:
note: std::basic_ostream<char, _Traits>&
std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits
= std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/ostream:447:
note: std::basic_ostream<_CharT, _Traits>&
std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with
_CharT = char, _Traits = std::char_traits<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::__normal_iterator<float*,
std::vector<float, std::allocator<float> > >, _Function =
std::pointer_to_unary_function<std::vector<float, std::allocator<float>
, void>]':

.../Utils.h:41: instantiated from 'void printList(T&) [with T =
std::vector<float, std::allocator<float> >]'
.../BlockList.cpp:444: 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_function<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>::operator()(_Arg) const [with _Arg = std::vector<float,
std::allocator<float> >, _Result = void]

Feb 21 '06 #4
TB
Pl********@gmail.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<typename 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********@gmail.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::basic_ostream<char, std::char_traits<char> >&)'

Feb 22 '06 #7
Pl********@gmail.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>(os)" 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(ostream& 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::basic_ostream<char, std::char_traits<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********@gmail.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::basic_ostream<char, std::char_traits<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<typename T>
struct print : public std::unary_function<T,void> {
void operator()(T item) {
d_os << item << " ";
}
std::ostream & d_os;
print(std::ostream & os) : d_os(os) {}
};

template<typename T>
void printList(T const & list, std::ostream & os) {
std::for_each(list.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
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...
10
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...
12
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...
19
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...
2
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) ...
0
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
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...
5
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...
6
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...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...

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.