[英]Compilation error in explicitly instantiated template methods
I've two different template classes. 我有两个不同的模板类。 One of them has a member function that returns a pointer to an object of the other template class.
其中一个具有成员函数,该成员函数返回指向另一个模板类的对象的指针。 Currently, I'm not able to compile the code below and any suggestions are really welcome.
目前,我无法编译下面的代码,真的很欢迎任何建议。
#include <stdio.h>
#include <stdlib.h>
#include <foo.h>
#include <bar.h>
int main(int argc, char **argv){
...
int nParam;
...
CFoo<float> * pFoo = NULL;
pFoo = new CFoo<float>();
CBar<float> * pBar = NULL;
pBar = pFoo->doSomething(nParam); // error: no matching function for call to ‘CFoo<float>::doSomething(int)’
...
delete pFoo;
delete pBar;
return (0);
}
#include <bar.h>
template < class FOO_TYPE >
class CFoo{
public:
...
template < class BAR_TYPE >
CBar<BAR_TYPE> * doSomething(int);
...
};
template < class FOO_TYPE >
template < class BAR_TYPE >
CBar<BAR_TYPE> * CFoo<FOO_TYPE>::doSomething(int nParam){
...
}
#include "foo-impl.inc"
template class CFoo<float>;
template class CFoo<double>;
template CBar<float>* CFoo<float>::doSomething( int );
template CBar<double>* CFoo<float>::doSomething( int );
template CBar<double>* CFoo<double>::doSomething( int );
template CBar<float>* CFoo<double>::doSomething( int );
/*
I also tried the explicit instantiation in the last line, but I get the error below:
template-id ‘doSomething<CBar<float>*>’ for ‘CBar<float>* CFoo<float>::doSomething(int)’ does not match any template declaration
*/
// template CBar<float>* CFoo<float>::doSomething < CBar<float> * > ( int );
Consider that I need to call the doSomething
method inside of a member function
of a third class. 考虑到我需要在第三类的
member function
内部调用doSomething
方法。
template < class FOO_TYPE, class BAR_TYPE >
void CMyClass<FOO_TYPE, class BAR_TYPE>::doSomeWork(CFoo<FOO_TYPE> * pFoo){
...
int nParam;
...
CBar<BAR_TYPE> * pBar = NULL;
pBar = pFoo->doSomething<BAR_TYPE>(nParam); //error: expected primary-expression before ‘>’ token
delete pBar;
...
}
Note that, I had a similar problem that I posted a while ago in this forum , but by trying to adapt the suggestion to my code, I couldn't solve the bug. 请注意,我有一个类似的问题,我在不久前在该论坛上发布过 ,但是通过尝试使建议适应我的代码,我无法解决该错误。 I hope, my post is valid.
希望我的帖子有效。
The compiler can't deduce the template arguments -- it doesn't know whether you want to call CFoo<float>::doSomething<float>(int)
, CFoo<float>::doSomething<unsigned int>(int)
, or whatever. 编译器无法推断出模板参数-它不知道您是否要调用
CFoo<float>::doSomething<float>(int)
, CFoo<float>::doSomething<unsigned int>(int)
,管他呢。 So, you have to explicitly tell it what the template arguments are: 因此,您必须明确告诉它模板参数是什么:
// Explicitly call the function with BAR_TYPE=float
pBar = pFoo->doSomething<float>(1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.