"错误:在构造未初始化的结构时没有用于调用的匹配函数"

5 c++ constructor struct

我正在尝试使用boost::lockfree::spsc_queue这个websocket服务器而不是std::queuefor 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;
};
Run Code Online (Sandbox Code Playgroud)

我无法初始化此struct内联

action a = m_actions.front();
Run Code Online (Sandbox Code Playgroud)

因为spsc_queue没有该功能但用于void pop设置对象和return boolean循环.

当我尝试

action a;
while(m_actions.pop(a)){
    ...
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

如何action构建然后设置spsc_queue.pop()

Pie*_*aud 5

这是因为您的action类中没有默认构造函数。它是可以不带参数调用的构造函数

但是当你这样做时:

action a;
Run Code Online (Sandbox Code Playgroud)

你需要这个构造函数:

struct action {
    action();  // Default constructor
    // ...
};
Run Code Online (Sandbox Code Playgroud)

您应该声明并定义它。

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