Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数C+;中的自动变量出错+; 在C++中函数之间传递一个值时,我遇到了一些问题。我在下面添加代码。在mqttReceive中,接收JSON格式的MQTT消息,并在send()中再次发送该消息,以便在void send()中接收。但是,我尝试将收到的消息声明为自动,但它不起作用。我错过了什么_C++_Function_Auto - Fatal编程技术网

函数C+;中的自动变量出错+; 在C++中函数之间传递一个值时,我遇到了一些问题。我在下面添加代码。在mqttReceive中,接收JSON格式的MQTT消息,并在send()中再次发送该消息,以便在void send()中接收。但是,我尝试将收到的消息声明为自动,但它不起作用。我错过了什么

函数C+;中的自动变量出错+; 在C++中函数之间传递一个值时,我遇到了一些问题。我在下面添加代码。在mqttReceive中,接收JSON格式的MQTT消息,并在send()中再次发送该消息,以便在void send()中接收。但是,我尝试将收到的消息声明为自动,但它不起作用。我错过了什么,c++,function,auto,C++,Function,Auto,cpp: 错误: 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; |

cpp:

错误:

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
包含在/home/mqtt_application.cpp:1中的文件中:
/home/mqtt_application.hpp:26:24:警告:在参数声明中使用“auto”仅适用于“-fconcepts ts”
26 |无效发送(const auto&msg)覆盖;
|                        ^~~~
/home/mqttu应用程序。hpp:26:35:错误:成员模板“void MqttApplication::send(const auto:1&)”可能没有virt说明符
26 |无效发送(const auto&msg)覆盖;
|                                   ^~~~~~~~
/home/mqtt_application.cpp:320:34:警告:在参数声明中使用“auto”仅适用于“-fconcepts ts”
320 |无效MqttApplication::发送(自动和消息)
|                                  ^~~~
/home/mqtt_application.cpp:320:6:错误:没有与“void MqttApplication::send(auto:2&)”匹配的声明
320 |无效MqttApplication::发送(自动和消息)
|      ^~~~~~~~~~~~~~~~~~
在/home/mqtt_application.cpp中包含的文件中:1:
/home/mqtt_application.hpp:26:10:注意:候选者是:“模板void MqttApplication::send(const auto:1&)”
26 |无效发送(const auto&msg)覆盖;
|          ^~~~~~~
在/home/mqtt_application.cpp中包含的文件中:1:
/home/mqtt_application.hpp:15:7:注意:此处定义了“类MqttApplication”
15 | MQT类应用:公共应用
|       ^~~~~~~~~~~~~~~~~~
tools/mqtt.dir/build.make:62:目标'tools/mqtt.dir/mqtt_application.cpp.o'的配方失败
make[2]:***[tools/mqtt.dir/mqtt_application.cpp.o]错误1
CMakeFiles/Makefile2:834:目标“tools/mqtt.dir/all”的配方失败
生成[1]:***[tools/mqtt.dir/all]错误2
Makefile:129:目标“全部”的配方失败
make:**[全部]错误2

我正在用C++14进行编译。我尝试了各种配置,比如字符串、int等等。输入是一个常规的JSON字符串。谢谢

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


<>如果编译器理解概念,理解概念,那么你就可以构建代码,但是这需要你理解一个主要的全新C++特性。

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


如果编译器理解概念,理解概念,那么你就可以构建代码,但是这需要你理解一个主要的全新C++特性。

< P>使用<代码> Audio<代码>作为参数,一些编译器在早期版本中支持它作为扩展,但在早期版本中它不是ISO。

使用
auto
作为参数是a,一些编译器在早期版本中支持它作为扩展,但在早期版本中它不是ISO。

此函数模板语法需要C++20。对于C++17及更高版本,您需要更改以下内容:

void MqttApplication::send(auto msg)
致:

模板
void MqttApplication::send(T msg)

两者都是等效的,但C++20版本较短。请参阅。

函数模板的此语法需要C++20。对于C++17及更高版本,您需要更改以下内容:

void MqttApplication::send(auto msg)
致:

模板
void MqttApplication::send(T msg)

两者都是等效的,但C++20版本较短。请参阅。

编译器需要知道
msg
的确切类型。Auto不是msg的正确类型?@编码器,
Auto
不是类型。它是一个占位符,在特定的上下文中可以告诉编译器推断类型。试着把auto想象成一个别名,在cpp文件中,编译器可以从
mqttClient->consume_message()
推断返回的类型,因此很高兴调用auto。但是,在hpp中,当为
::send(…)
定义函数签名时,编译器没有任何操作。如果要发送为泛型,则可以考虑模板<代码>发送<代码>。您需要更具体。编译器需要知道代码的确切类型> MSG。AUTO不是MSG的正确类型?@ CooDead,<代码> Audio>代码>不是一种类型。它是一个占位符,在特定的上下文中可以告诉编译器推断类型。试着把auto想象成一个别名,在cpp文件中,编译器可以从
mqttClient->consume_message()
推断返回的类型,因此很高兴调用auto。但是,在hpp中,当为
::send(…)
定义函数签名时,编译器没有任何操作。如果你想发送为泛型,你可以考虑模板<代码>发送<代码>。代码>无效f(自动)只是一个普通模板
void f(一些概念车)
会涉及概念。不过这里没有涉及概念<代码>无效f(自动)只是一个普通模板<代码>void f(一些概念自动)会涉及概念。我可以使用C++11声明自动变量,但我不能在此版本中作为参数传递?我的意思是,我只需要将自动消息传递给send函数。@您需要将
msg
传递给可以接受
msg
类型的函数的代码
auto
不是一种类型,因此您不能有一个函数将
auto
作为某种类型接受。为此,您需要一个函数模板,或者一个接受
std::any
的函数。永远记住C++是静态类型的语言。全部的
void MqttApplication::send(auto msg)
template <typename T>
void MqttApplication::send(T msg)