类别定义:
template<class K, class V,
         unsigned hashFunc(const K&),
         int compFunc(const K&,const K&)=&_compFunc<K> > class X {};我想在类代码块之外定义一个类方法。就像这样:
template<class K, class V,
         unsigned hashFunc(const K&),
         int compFunc(const K&,const K&)=&_compFunc<K> >
X<K, V, hashFunc, compFunc>::X() { }g++ v.4.4.3返回
错误:包含“X::X()”的类模板参数的默认参数
为什么编译器会抱怨,我如何使它工作呢?
发布于 2010-11-19 17:28:50
您没有为X声明或定义构造函数。此外,您还在尝试的X::X定义中重复了默认模板参数。
这是固定代码,main-ified:
template<class K, class V,
         unsigned hashFunc(const K&),
         int compFunc(const K&,const K&)=&_compFunc<K> > 
class X 
{ 
    X();
};
template<class K, class V,
         unsigned hashFunc(const K&),
         int compFunc(const K&,const K&) >
X<K, V, hashFunc, compFunc>::X() { }
int main()
{
}发布于 2010-11-19 17:28:27
您不应该重复默认的模板参数:
template<class K, class V,
         unsigned hashFunc(const K&),
         int compFunc(const K&,const K&)>
X<K, V, hashFunc, compFunc>::X() { /* ... */ }正如John所指出的,类X显然也必须声明构造函数,但为了清楚起见,我假设代码被删除了。
https://stackoverflow.com/questions/4227659
复制相似问题