echo示例编译错误:对重载函数的模糊调用

时间:2016-10-14 10:56:53

标签: c++ c++11 visual-studio-2015

我在一些名为uWebSockets的开源库中有这些函数 我尝试使用Visual Studio 2013运行其简单示例的任何方式。

我尝试调用的函数如下所示:

template <bool isServer>
void Group<isServer>::onMessage(std::function<void (WebSocket<isServer>, char *, size_t, OpCode)> handler) {
    messageHandler = handler;
}

,示例如下:

int main()
{
    uWS::Hub h;


    h.onMessage([](uWS::WebSocket<uWS::SERVER> ws, char *message, size_t length, uWS::OpCode opCode) {
        ws.send(message, length, opCode);
    });

    h.listen(3000);
    h.run();
}

我得到的错误如下:

1>D:\dev\cpp\server\uWebSockets\git\uWebSockets\uWebSockets\examples\echo.cpp(9): error C2668: 'uWS::Group<false>::onMessage' : ambiguous call to overloaded function
1>          d:\dev\cpp\server\uwebsockets\git\uwebsockets\uwebsockets\src\Group.h(69): could be 'void uWS::Group<false>::onMessage(std::function<void (uWS::WebSocket<false>,char *,size_t,uWS::OpCode)>)'
1>          d:\dev\cpp\server\uwebsockets\git\uwebsockets\uwebsockets\src\Group.h(69): or       'void uWS::Group<true>::onMessage(std::function<void (uWS::WebSocket<true>,char *,size_t,uWS::OpCode)>)'
1>          while trying to match the argument list '(main::<lambda_f789fc974d6d87c4a2444e6ded66c2a0>)'

问题:
如果不将Visual Studio升级到2015,我可以解决错误吗? 或者它与编译器版本无关?

更新: 如果我将其设置为:h.onMessage<true>h.onMessage<uWS::SERVER>
我越来越 : 这个错误:

1>D:\dev\cpp\server\uWebSockets\git\uWebSockets\uWebSockets\examples\echo.cpp(10): error C2678: binary '>' : no operator found which takes a left-hand operand of type 'bool' (or there is no acceptable conversion)
1>          could be 'built-in C++ operator>(void (__cdecl *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode), void (__cdecl *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode))'
1>          or       'built-in C++ operator>(void (__stdcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode), void (__stdcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode))'
1>          or       'built-in C++ operator>(void (__fastcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode), void (__fastcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode))'
1>          or       'built-in C++ operator>(void (__vectorcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode), void (__vectorcall *)(uWS::WebSocket<true>,char *,size_t,uWS::OpCode))'
1>          while trying to match the argument list '(bool, main::<lambda_f789fc974d6d87c4a2444e6ded66c2a0>)'

1 个答案:

答案 0 :(得分:0)

我遇到了同样的错误,经过小修改后我就开始运行了:

int main()
{   uWS::Hub h;

   h.onMessage([](uWS::WebSocket<uWS::SERVER>* ws, char *message, size_t length, uWS::OpCode opCode) {
        ws->send(message, length, opCode);
    });

    h.listen(3000);
    h.run();
}