显式实例化模板方法中的编译错误

Jav*_*ier 1 c++ templates instantiation

我有两个不同的模板类.其中一个具有成员函数,该函数返回指向另一个模板类的对象的指针.目前,我无法编译下面的代码,我们非常欢迎任何建议.

main.cpp中

#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);
}
Run Code Online (Sandbox Code Playgroud)

foo.h中

#include <bar.h>

template < class FOO_TYPE >
class CFoo{

    public:

        ...

        template < class BAR_TYPE >
        CBar<BAR_TYPE> * doSomething(int);
        ...
};
Run Code Online (Sandbox Code Playgroud)

Foo.cpp中

template < class FOO_TYPE >
template < class BAR_TYPE >
CBar<BAR_TYPE> * CFoo<FOO_TYPE>::doSomething(int nParam){
    ...
}
#include "foo-impl.inc" 
Run Code Online (Sandbox Code Playgroud)

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 ); 
Run Code Online (Sandbox Code Playgroud)

考虑一下我需要在第三个类中调用该doSomething方法member function.

myclass.cpp

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;
    ...
} 
Run Code Online (Sandbox Code Playgroud)

请注意,我之前在此论坛中发布了类似的问题,但是通过尝试将该建议改编为我的代码,我无法解决该问题.我希望,我的帖子是有效的.

Ada*_*eld 5

编译器无法推断出模板参数-它不知道是否要打电话CFoo<float>::doSomething<float>(int),CFoo<float>::doSomething<unsigned int>(int)或什么的.所以,你必须明确告诉它模板参数是什么:

// Explicitly call the function with BAR_TYPE=float
pBar = pFoo->doSomething<float>(1);
Run Code Online (Sandbox Code Playgroud)