error: no matching function for call to 'uWS::TemplatedApp<false>::ws<main()::UserData>(const cha...

SinoDawn@stackoverflow(myself)

I'm using uwebsockets to create ws server.

main.cpp:

int main()
{
  struct UserData {

  };

  uWS::App().ws<UserData>("/*", {

      /* Just a few of the available handlers */
      .open = [](auto *ws) {
          /* MQTT syntax */
          ws->subscribe("sensors/+/house");
      },
      .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
          ws->send(message, opCode);
      }

  }).listen(9001, [](auto *listenSocket) {

      if (listenSocket) {
          std::cout << "Listening on port " << 9001 << std::endl;
      }

  }).run();
  return 0;
}

the error message while building:

……main.cpp:22:4: note:   cannot convert '{<lambda closure object>main()::<lambda(auto:11*)>{}, <lambda closure object>main()::<lambda(auto:12*, std::string_view, uWS::OpCode)>{}}' (type '<brace-enclosed initializer list>') to type 'uWS::TemplatedApp<false>::WebSocketBehavior<main()::UserData>&&'
   }).listen(9001, [](auto *listenSocket) {
    ^

env:

OS: Windows10 64bit

IDE: QtCreator

compiler: MinGW 8.1.0 32bit

c++std: 17

uwebsockets: 19.2.0

1 Answer

It is because the compiler cannot convert 'brace-enclosed initializer list' to T&&. It can be solved by declaring the struct and converting it to Rvalue reference.

  struct UserData {

  };

  uWS::TemplatedApp<false>::WebSocketBehavior<UserData> wsb = {
    /* Just a few of the available handlers */
    .open = [](auto *ws) {
      /* MQTT syntax */
      ws->subscribe("sensors/+/house");
    },
    .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
      ws->send(message, opCode);
    }
  };

  uWS::App().ws<UserData>("/*", std::move(wsb)).listen(9001, [](auto *listenSocket) {
      if (listenSocket) {
          std::cout << "Listening on port " << 9001 << std::endl;
      }
  }).run();

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容