Click to See Complete Forum and Search --> : template template parameter issue


SamWo123
February 7th, 2008, 04:35 PM
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>'



#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);

}

Graham
February 7th, 2008, 04:53 PM
For one thing std::vector takes two template arguments (contained type and allocator), so it doesn't match the function argument.

SamWo123
February 7th, 2008, 04:59 PM
Thanks Graham,


//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