我有以下代码,它可以正常工作:
list <Politician> MakeList ( int num, ... ){
va_list arguments;
va_start ( arguments, num );
list <Politician> PoliticianList;
for ( int x = 0; x < num; x++ ) {
PoliticianList.push_back(va_arg (arguments, Politician));
}
va_end ( arguments );
return PoliticianList;
}
然而,当试图使其通用化时:
template<class TYPE>
list <TYPE> MakeList ( int num, ... ){
va_list arguments;
va_start ( arguments, num );
list <TYPE> PoliticianList;
for ( int x = 0; x < num; x++ ) {
PoliticianList.push_back(va_arg (arguments, TYPE));
}
va_end ( arguments );
return PoliticianList;
}
编译时我遇到了以下错误:
error C2783: 'std::list<TYPE> MakeList(int,...)' : could not deduce template argument for 'TYPE'
如何使代码通用化,以便不必为不同的类对象重新实现相同的功能?
MakeList<Foo>(3, foo1, foo2, foo3)
。 - Kerrek SBMakeList
? - masoud