gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:40:35 31 4
gpt4 key购买 nike

<分区>

我正在尝试测试下面的代码但出现编译错误:

TemplateTest.cpp:44:13: error:expected primary-expression before 'int'
        h_.handle<int>(i);

构建命令:

g++ -c -std=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;
}

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com