简体   繁体   English

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

[英]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. 目前,我无法编译下面的代码,真的很欢迎任何建议。

main.cpp 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);
}

foo.h foo.h

#include <bar.h>

template < class FOO_TYPE >
class CFoo{

    public:

        ...

        template < class BAR_TYPE >
        CBar<BAR_TYPE> * doSomething(int);
        ...
};

foo.cpp foo.cpp

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

foo-impl.inc 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方法。

myclass.cpp 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;
    ...
} 

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.

相关问题 为什么C ++显式实例化的模板方法不能覆盖虚方法? - Why can't C++ explicitly-instantiated template methods override virtual methods? 模板方法在哪里实例化? - Where are template methods instantiated? 显式实例化模板类的显式实例化模板方法 - Explicitly instantiate template method of explicitly instantiated template class 未定义的对显式实例化模板函数的引用 - Undefined reference to explicitly instantiated template function 显式实例化类模板中的自动构造函数 - Automatic constructor in explicitly instantiated class template 从 dll 导入显式实例化的模板 class - Importing explicitly instantiated template class from dll 将显式实例化的函数模板与转换匹配 - Matching explicitly instantiated function template with conversion 如何在Visual Studio中导出从显式实例化模板派生的类? - How to export a class derived from an explicitly instantiated template in Visual Studio? 通过转换运算符调用显式实例化的模板函数 - Call of explicitly instantiated template function through conversion operator 除非显式专用,否则未实例化模板类的静态成员? - Static member of template class not instantiated unless explicitly specialized?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM