我正在尝试使用boost::lockfree::spsc_queue
websocket server代替std::queue
m_actions
来包含此struct
:
enum action_type {
SUBSCRIBE,
UNSUBSCRIBE,
MESSAGE
};
struct action {
action(action_type t, connection_hdl h) : type(t), hdl(h) {}
action(action_type t, server::message_ptr m) : type(t), msg(m) {}
action_type type;
websocketpp::connection_hdl hdl;
server::message_ptr msg;
};
我无法使用
内联初始化此struct
action a = m_actions.front();
因为spsc_queue
没有该功能,但使用void pop
来设置对象和return boolean
进行循环。
当我尝试
时action a;
while(m_actions.pop(a)){
...
gcc
说:
position_server.cpp:106:11: error: no matching function for call to ‘action::action()’
position_server.cpp:106:11: note: candidates are:
position_server.cpp:39:5: note: action::action(action_type, websocketpp::endpoint<websocketpp::connection<websocketpp::config::asio>, websocketpp::config::asio>::message_ptr)
position_server.cpp:39:5: note: candidate expects 2 arguments, 0 provided
position_server.cpp:38:5: note: action::action(action_type, websocketpp::connection_hdl)
position_server.cpp:38:5: note: candidate expects 2 arguments, 0 provided
position_server.cpp:37:8: note: action::action(const action&)
position_server.cpp:37:8: note: candidate expects 1 argument, 0 provided
如何构建action
,然后使用spsc_queue.pop()
进行设置?
答案 0 :(得分:4)
这是因为您的action
课程中没有默认构造函数。 可以不带参数调用的构造函数。
但是当你这样做时:
action a;
你需要这个构造函数:
struct action {
action(); // Default constructor
// ...
};
您应该声明并定义它。
当声明没有参数列表的对象值时,将自动调用默认构造函数。 (例如action a;
)。