|
-
February 7th, 2008, 05:35 PM
#1
template template parameter issue
I am wondering what's wrong with template parameter in print()?
I was tring to use print without specifying template argument.
This is the error I got.
error C2784: 'void print(const C<T> &)' : could not deduce template argument for 'const C<T> &' from 'std::vector<_Ty>'
Code:
#include <vector>
#include<iostream>
#include <iterator>
using namespace std;
template<typename T, template<class > class C >
void print(const C<T> &x)
{
copy(x.begin(), x.end(), ostream_iterator<T>(cout," "));
cout << endl;
}
int main()
{
vector<double> v;
v.push_back(100);
print(v);
}
-
February 7th, 2008, 05:53 PM
#2
Re: template template parameter issue
For one thing std::vector takes two template arguments (contained type and allocator), so it doesn't match the function argument.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 7th, 2008, 05:59 PM
#3
Re: template template parameter issue
Thanks Graham,
Code:
//correct defition
template<typename T, template<class U, class Allocator = allocator<U> > class C>
void print(const C<T> &x)
{
copy(x.begin(), x.end(), ostream_iterator<T>(cout," "));
cout << endl;
}
Sam
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|