相关疑难解决方法(0)

模板:模板函数与类的模板成员函数不兼容

这是我实际拥有的一些代码的最小测试用例.它在尝试评估时失败a.getResult<B>():

test.cpp: In function 'void printStuff(const A&)':
test.cpp:6: error: expected primary-expression before '>' token
test.cpp:6: error: expected primary-expression before ')' token
Run Code Online (Sandbox Code Playgroud)

代码是:

#include <iostream>

template< class A, class B>
void printStuff( const A& a)
{
    size_t value = a.getResult<B>();
    std::cout << value << std::endl;
}

struct Firstclass {
    template< class X >
    size_t getResult() const {
        X someInstance;
        return sizeof(someInstance);
    }
};

int main(int, char**) {
    Firstclass foo;

    printStuff<Firstclass, short int>(foo);
    printStuff<Firstclass, double>(foo);

    std::cout << foo.getResult< double >() …
Run Code Online (Sandbox Code Playgroud)

c++ templates

22
推荐指数
2
解决办法
3725
查看次数

标签 统计

c++ ×1

templates ×1