std :: vector作为模板参数...所有的地狱打破输 [英] std::vector as template parameter... all hell breaks lose
问题描述
我很快爆炸...有人请指出目前的错误:
模板< typename TType,template< typename ...> class Container,class Comparer>
Container< TType> * sort(const Container< TType>& container){
...
}
问题出现时,我试图调用此函数与std :: vector作为其Container参数。我得到以下错误:
main.cpp:24:34:错误:没有匹配函数调用'func '
main.cpp:24:34:note:candidate is:
main.cpp:14:6:note:template< class T,template< class ...> class Container> void func()
main.cpp:14:6:note:模板参数扣除/替换失败:
$ b b
下面是我如何调用它:
std :: vector< int> * m ; int,std :: vector< int>,Comparer>(m));
当我从函数中删除模板模板参数时,它工作,使用MinGW附带的最新g ++编译器。 IDE是NetBeans 7.3,不应该影响太多。编译器参数为:
-std = c ++ 11-Wall -pedantic
感谢您的帮助,
- Joey解决方案>您应该提供一个模板,而不是从模板创建的特定类型。正确的调用将是:
sort< int,std :: vector,Comparer>(m)
请注意,
sort
本身提供Container
,如const Container< TType>&
。清楚地将Container
设置为std :: vector< int>
你会要求编译器执行std :: vector< int>< int>
I'm exploding soon... Someone please point out what is wrong in here currently:
template <typename TType, template <typename ...> class Container, class Comparer> Container<TType>* sort(const Container<TType>& container) { ... }
The problem comes when I try to call this function with std::vector as its Container parameter. I get the following errors:
main.cpp:24:34: error: no matching function for call to 'func()' main.cpp:24:34: note: candidate is: main.cpp:14:6: note: template<class T, template<class ...> class Container> void func() main.cpp:14:6: note: template argument deduction/substitution failed:
And here's how I'm trying to call it:
std::vector<int>* m(sort<int, std::vector<int>, Comparer>(m));
When I remove the template template parameter from the function it works, but not with it... I'm using the latest g++ compiler that comes with MinGW. The IDE is NetBeans 7.3 tho that shouldn't affect much. Compiler arguments are:
-std=c++11 -Wall -pedantic
Thanks for every help, - Joey
解决方案You're supposed to provide a template, not a specific type created from a template. The correct call would be:
sort<int, std::vector, Comparer>(m)
Note that
sort
itself is providing the template arguments forContainer
, as inconst Container<TType>&
. Clearly settingContainer
tostd::vector<int>
makes no sense; you'd be asking the compiler to do something likestd::vector<int><int>
这篇关于std :: vector作为模板参数...所有的地狱打破输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文