函数C ++中的Auto变量出错

问题描述

在C ++中的函数之间传递值时遇到问题。我在下面添加代码。在mqttReceive中,将接收JSON中的MQTT消息,并将其再次在send()中发送,以在void send()中接收。但是,我试图将接收到的消息声明为自动,但是它不起作用。我想念的是什么?

cpp:

void MqttApplication::mqttReceive()
{
    try {
        
        mqttClient->start_consuming();
        mqttClient->subscribe(TOPIC,QOS)->wait();
        
    }
    catch (const mqtt::exception& exc) {
        cerr << exc.what() << endl;
        return;
    }

    
    while (true) {
        auto msg = mqttClient->consume_message();   
        

        try {               
            send(msg);
        }
        catch (const mqtt::exception& exc) {
            cerr << exc.what() << endl;
            return;
        }

        if (msg->get_topic() == "command" &&
                msg->to_string() == "exit") {
            cout << "Exit command received" << endl;
            break;
        }

        cout << msg->get_topic() << ": " << msg->to_string() << endl;
    }
}



void MqttApplication::send(auto msg)
{
    ...
}

hpp:

class MqttApplication : public Application
    {
    private:    
    
        void send(const auto msg);
    
        void mqttReceive();
        

错误

In file included from /home/mqtt_application.cpp:1:
/home/mqtt_application.hpp:26:24: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts-ts’
   26 |     void send(const auto& msg) override;
      |                        ^~~~
/home/mqtt_application.hpp:26:35: error: member template ‘void MqttApplication::send(const auto:1&)’ may not have virt-specifiers
   26 |     void send(const auto& msg) override;
      |                                   ^~~~~~~~
/home/mqtt_application.cpp:320:34: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts-ts’
  320 | void MqttApplication::send(auto& msg)
      |                                  ^~~~
/home/mqtt_application.cpp:320:6: error: no declaration matches ‘void MqttApplication::send(auto:2&)’
  320 | void MqttApplication::send(auto& msg)
      |      ^~~~~~~~~~~~~~~~~~
In file included from /home/mqtt_application.cpp:1:
/home/mqtt_application.hpp:26:10: note: candidate is: ‘template<class auto:1> void MqttApplication::send(const auto:1&)’
   26 |     void send(const auto& msg) override;
      |          ^~~~~~~
In file included from /home/mqtt_application.cpp:1:
/home/mqtt_application.hpp:15:7: note: ‘class MqttApplication’ defined here
   15 | class MqttApplication : public Application
      |       ^~~~~~~~~~~~~~~~~~
tools/mqtt.dir/build.make:62: recipe for target 'tools/mqtt.dir/mqtt_application.cpp.o' Failed
make[2]: *** [tools/mqtt.dir/mqtt_application.cpp.o] Error 1
CMakeFiles/Makefile2:834: recipe for target 'tools/mqtt.dir/all' Failed
make[1]: *** [tools/mqtt.dir/all] Error 2
Makefile:129: recipe for target 'all' Failed
make: *** [all] Error 2

我正在使用C ++ 14进行编译。我尝试了所有类型的配置,字符串,整数等。输入是常规JSON字符串。谢谢

解决方法

使用auto作为参数是C++20 feature,一些编译器支持它作为早期版本的扩展,但在那里没有ISO。

,

此函数模板的语法需要C ++ 20。对于C ++ 17和更早的版本,您需要对此进行更改:

void MqttApplication::send(auto msg)

收件人:

template <typename T>
void MqttApplication::send(T msg)

两者都是等效的,但C ++ 20版本较短。参见Abbreviated function template

,

我想知道您期望发生什么?在函数声明中使用“ auto”时,编译器无法知道类型是什么,因此拒绝编译。这与“ auto x = 3;”不同,编译器知道x的类型应该与3的类型相同。

如果您的编译器理解概念并且您理解概念,则可以进行代码构建,但这需要您了解主要的全新C ++功能。