gpt4 book ai didi

c++ - 实例化模板函数时出现编译器错误

转载 作者:行者123 更新时间:2023-11-30 04:05:52 28 4
gpt4 key购买 nike

我正在尝试使用 g++ v.4.8.1 编译以下代码:

#include <iostream>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/euml/euml.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/msm/back/state_machine.hpp>

typedef boost::msm::front::none none;

namespace events{
class A{
double member;

template <typename T>
struct field{
typedef T field_type;
};

public:
typedef field<double> A_double;
typedef field<float> A_float;
typedef field<int> A_int;

A() : member(M_PI){}
A(const double& d) : member(d){}

double get_member() const{ return member; }

template<typename T>
typename T::field_type extract() const{
typedef typename T::field_type ret;
return static_cast<ret>(member);
}
};

struct B{};
}

struct front : public boost::msm::front::state_machine_def<front>{
// ----------------------- States ----------------------------
struct S1 : public boost::msm::front::state<>{

template <class Event, class Fsm>
void on_entry(Event const&, Fsm& fsm) {
std::cout << "executing S1::on_entry()" << std::endl;
}

template <class Event, class Fsm>
void on_exit(Event const&, Fsm& fsm) {
std::cout << "executing S1::on_exit()" << std::endl;
}
};

struct S2 : public boost::msm::front::state<>{
template <class Event, class Fsm>
void on_entry(Event const& evt, Fsm& fsm) {
std::cout << "executing S2::on_entry()" << std::endl;
// Initialize a
fsm.a = static_cast<events::A>(evt);
}

template <class Event, class Fsm>
void on_exit(Event const&, Fsm& fsm) {
std::cout << "executing S2::on_exit()" << std::endl;
std::cout << "a.get_member(): " << fsm.a.get_member() << std::endl; // OK
int m = fsm.a.extract< events::A::A_int >(); // ERROR
std::cout << "a.extract<events::A::A_int>(): " << m << std::endl;
}
};

// --------------------------------- Initial state ------------------------------
typedef S1 initial_state;

// --------------------------------- Transition table ----------------------------------
struct transition_table : boost::mpl::vector<
// Start Event Target Action Guard
_row< S1, events::A, S2 >,
_row< S2, events::A, S2 >,
_row< S2, events::B, S1 >
> {};

template <class Fsm, class Event>
void no_transition(Event const& e, Fsm&, int state){
std::cout << "No transition found" << std::endl;
}

// Internal events::A member
events::A a;
};

typedef boost::msm::back::state_machine<front> back;

static char const* const state_names[] = { "S1", "S2" };
void pstate(back const& p){
std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
}

int main(){
back b;
events::A tmp;
std::cout << tmp.extract<events::A::A_double>() << std::endl;
b.start();
pstate(b);
std::cout << std::endl << "Firing event A()" << std::endl;
b.process_event(events::A());
pstate(b);
std::cout << std::endl << "Firing event A(153.13)" << std::endl;
b.process_event(events::A(153.13));
pstate(b);
std::cout << std::endl << "Firing event A(14)" << std::endl;
b.process_event(events::A(14));
pstate(b);
b.stop();
return 0;
}

我收到以下错误:

main.cpp: In member function ‘void front::S2::on_exit(const Event&, Fsm&)’:
main.cpp:67:44: error: expected primary-expression before ‘>’ token
int m = fsm.a.extract< events::A::A_int >(); // ERROR
^
main.cpp:67:46: error: expected primary-expression before ‘)’ token
int m = fsm.a.extract< events::A::A_int >(); // ERROR
^

我已经尝试用以下一行替换该行:

int m = static_cast<events::A>(fsm.a).extract<events::A::A_int>();    // OK

现在代码可以按预期编译和工作。
谁能帮我理解为什么?

最佳答案

使用

int m = fsm.a.template extract< events::A::A_int >();

相关:template member function of template class called from template function

关于c++ - 实例化模板函数时出现编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23107577/

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