作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个不同的模板类。其中一个有一个成员函数,它返回一个指向另一个模板类的对象的指针。目前,我无法编译下面的代码,非常欢迎任何建议。
#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 );
考虑到我需要在第三个类的 成员函数
中调用 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;
...
}
请注意,我有 a similar problem that I posted a while ago in this forum ,但是通过尝试使建议适应我的代码,我无法解决该错误。我希望,我的帖子是有效的。
最佳答案
编译器无法推断模板参数——它不知道你是否想调用 CFoo<float>::doSomething<float>(int)
, CFoo<float>::doSomething<unsigned int>(int)
, 管他呢。因此,您必须明确告诉它模板参数是什么:
// Explicitly call the function with BAR_TYPE=float
pBar = pFoo->doSomething<float>(1);
关于c++ - 显式实例化的模板方法中的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5251496/
我是一名优秀的程序员,十分优秀!