Search code examples
c++compiler-errorstype-conversion

Why does c++ compiler want to convert to int when type isn't declared correctly?


I was coding a websocket class, and wanted to have a function as parameter in the constructor. But I forgot a : on the declaration of the message_handler method in the WebsocketClient class.

The compiler kept giving me this error:

error: cannot convert 'std::string' {aka 'std::__cxx11::basic_string<char>'} to 'int'.

I was so confused but finally found the issue. But why does the compiler not tell me it can't resolve the type (which I guess was the issue because the syntax was wrong)?

This is the main code:

void ws_msg_handler(std::string &msg_recieved){
  std::cout << "ws_msg_handler: " << msg_recieved << std::endl;

}

WebsocketClient wsClient("ws://my.secret.ip.adress:7777", ws_msg_handler);

This is the class:

class WebsocketClient {

private:
    

    void message_handler(std:string &recieved_msg); // <- here is ":" missing

public:
    char *uri;


    WebsocketClient(char *uri, void (*message_handler)(std::string&)){
        this->uri = uri;
        this->message_handler = message_handler;



    };


    void ws_data_receive_handler(esp_websocket_event_data_t *data_recieved){
        
        std::string msg_recieved(data_recieved->data_ptr);
        
        this->message_handler(msg_recieved); // <- here it gives the error: "error: cannot convert 'std::string' {aka 'std::__cxx11::basic_string<char>'} to 'int'"

    }

};

I tried googling it, eventually asking ChatGPT. And when I gave the last piece of info, it was able to get out the syntax error.


Solution

  • why does the compiler not tell me it cant resolve the type?

    It told you as first error, I got:

    <source>:10:26: error: 'std' is not a type
       10 |     void message_handler(std:string &recieved_msg); // <- here is ":" missing
          |                          ^~~
    <source>:10:29: error: expected ',' or '...' before ':' token
       10 |     void message_handler(std:string &recieved_msg); // <- here is ":" missing
          |                             ^
    <source>: In member function 'void WebsocketClient::ws_data_receive_handler(esp_websocket_event_data_t*)':
    <source>:15:31: error: cannot convert 'std::string' {aka 'std::__cxx11::basic_string<char>'} to 'int'
       15 |         this->message_handler(msg_recieved); // <- here it gives the error: "error: cannot convert 'std::string' {aka 'std::__cxx11::basic_string<char>'} to 'int'"
          |                               ^~~~~~~~~~~~
          |                               |
          |                               std::string {aka std::__cxx11::basic_string<char>}
    <source>:10:26: note:   initializing argument 1 of 'void WebsocketClient::message_handler(int)'
       10 |     void message_handler(std:string &recieved_msg); // <- here is ":" missing
          |                          ^~~
    

    Demo

    To allow to diagnose several errors, compiler has to "recover" from the previous errors. Replacing unknown/mal-formed types by int/void might be a method. This might lead to report future error "wrongly" though.

    So you have to check for first error for accurate report.