模板成员函数调用 -- "error: expected primary-expression before 'int'"



我试图在下面测试代码,但要获得编译错误:

templatetest.cpp:44:13:错误:预期的主要表达在'int'
之前       nbsp; nbsp; nbsp; nbsp; h_.handle&lt< int>(i);

构建命令:

g -c -sstd = C 11 -G -O2 -WALL -WERROR TEMPLATETEST.CPP -O TEMPLATETEST.O

#include <iostream>
using namespace std;
enum PROTOCOL {
  PROTO_A,
  PROTO_B
};
// ----- HandlerClass -------
template <PROTOCOL protocol>
class Handler {
public:
    template <class TMsg>
    bool handle(const TMsg&) {
        return false;
    }
};
template <>
template <class TMsg>
bool Handler<PROTO_A>::handle(const TMsg&) {
    cout << "PROTO_A handler" << endl;
    return true;
}
template <>
template <class TMsg>
bool Handler<PROTO_B>::handle(const TMsg&) {
    cout << "PROTO_B handler" << endl;
    return true;
}
// ----- DataClass ------
template <PROTOCOL protocol>
struct Data {
    typedef Handler<protocol> H; //select appropriate handler
    H h_;
    int i;
    Data() : i() {}
    void f() {
            h_.handle<int>(i); //***** <- getting error here
    }
};
int main () {
    Data<PROTO_A> b;
    b.f();
    return 0;
}

在行下方是不正确的:

h_.handle<int>(i);

因为h_是模板类Handler<PROTOCOL>的实例。

将其更改为:

h_.template handle<int>(i);

您应该检查以下问题的答案:

我必须在何处以及为什么要放置"模板"one_answers" typename"关键字?

相关内容

最新更新