构造非初始化结构时的"error: no matching function for call to"

"error: no matching function for call to" when constructing an unintialized struct

本文关键字:matching no function for to call error 初始化 结构      更新时间:2023-10-16

我正在尝试使用此 websocket 服务器的boost::lockfree::spsc_queue,而不是m_actions包含此structstd::queue

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()设置?

这是因为

您的action类中没有默认构造函数。 它是可以在没有参数的情况下调用的构造函数

但是当你这样做时:

action a;

您需要此构造函数:

struct action {
    action();  // Default constructor
    // ...
};

您应该声明并定义它。

当声明没有参数列表的对象值时,将自动调用默认构造函数。(例如 action a; )。

相关文章: