gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:09:43 30 4
gpt4 key购买 nike

我在使用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++ - 函数C++中的Auto变量出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64534028/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com