创建将字符串数组作为参数传递的 class 函数的正确方法是什么? [阿杜伊诺]
What is the correct way to create a class function that is passed a string array as a parameter? [Arduino]
我想在 Arduino/ESP8266 的现有库中创建自己的函数,传递一个空数组以防用户没有任何 header.
//.h file
t_httpUpdate_return updateheader(WiFiClient& client, const String& url, const String& currentVersion = "", const String& header[][2] = {{}});
//.cpp file
HTTPUpdateResult ESP8266HTTPUpdate::updateheader(WiFiClient& client, const String& url, const String& currentVersion, const String& header[][2])
{
HTTPClient http;
http.begin(client,url);
for (int i, i < sizeof(header), i++){
http.addHeader(F(header[i][0]), header[i][1]);
}
return handleUpdate(http, currentVersion, false);
}
但是我在尝试编译时遇到下一个错误:
C:\Users\myuser\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266.7.1\libraries\ESP8266httpUpdate\src/ESP8266httpUpdate.h:125:143: error: declaration of 'header' as array of references
t_httpUpdate_return updateheader(WiFiClient& client, const String& url, const String& currentVersion = "", const String& header[][2] = {{}});
^
exit status 1
Error compilando para la tarjeta NodeMCU 1.0 (ESP-12E Module).
我正在使用 ESP8266httpUpdate Library。
不允许引用数组。另外,你不能像那样限制内部数组的大小。
数组不能作为值传递,当您将它们传递给另一个函数时,它们基本上会退化为指针。这也意味着您需要传递数组的大小。
你写的:sizeof(header)
是一个 bug - 你有一个 C-style 数组作为函数参数,数组衰减为一个指针和 sizeof(header)
给你 指针的大小 而不是数组的长度!
我会为实际的 header 使用一个结构,因为你总是想要 2 的大小,然后你只需要处理一个 one-dimensional 数组:
struct HttpHeader {
String name;
String value;
};
t_httpUpdate_return updateheader(WiFiClient& client, const String& url,
const String& currentVersion = "",
const HttpHeader* headers, int headers_size)
{
HTTPClient http;
http.begin(client,url);
for (int i = 0; i < headers_size; i++) {
http.addHeader(F(headers[i].name), headers[i].value);
}
return handleUpdate(http, currentVersion, false);
}
// Calling it somewhere:
HttpHeader headers[] = { { String("Accept"), String("application/json") } };
updateheader(client, version, headers, 1);
请注意,这意味着数组数据存在于调用函数的堆栈中,一旦该函数结束,指针就会失效。但是当不使用堆分配时总是这样。
我想在 Arduino/ESP8266 的现有库中创建自己的函数,传递一个空数组以防用户没有任何 header.
//.h file
t_httpUpdate_return updateheader(WiFiClient& client, const String& url, const String& currentVersion = "", const String& header[][2] = {{}});
//.cpp file
HTTPUpdateResult ESP8266HTTPUpdate::updateheader(WiFiClient& client, const String& url, const String& currentVersion, const String& header[][2])
{
HTTPClient http;
http.begin(client,url);
for (int i, i < sizeof(header), i++){
http.addHeader(F(header[i][0]), header[i][1]);
}
return handleUpdate(http, currentVersion, false);
}
但是我在尝试编译时遇到下一个错误:
C:\Users\myuser\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266.7.1\libraries\ESP8266httpUpdate\src/ESP8266httpUpdate.h:125:143: error: declaration of 'header' as array of references
t_httpUpdate_return updateheader(WiFiClient& client, const String& url, const String& currentVersion = "", const String& header[][2] = {{}});
^
exit status 1
Error compilando para la tarjeta NodeMCU 1.0 (ESP-12E Module).
我正在使用 ESP8266httpUpdate Library。
不允许引用数组。另外,你不能像那样限制内部数组的大小。
数组不能作为值传递,当您将它们传递给另一个函数时,它们基本上会退化为指针。这也意味着您需要传递数组的大小。
你写的:sizeof(header)
是一个 bug - 你有一个 C-style 数组作为函数参数,数组衰减为一个指针和 sizeof(header)
给你 指针的大小 而不是数组的长度!
我会为实际的 header 使用一个结构,因为你总是想要 2 的大小,然后你只需要处理一个 one-dimensional 数组:
struct HttpHeader {
String name;
String value;
};
t_httpUpdate_return updateheader(WiFiClient& client, const String& url,
const String& currentVersion = "",
const HttpHeader* headers, int headers_size)
{
HTTPClient http;
http.begin(client,url);
for (int i = 0; i < headers_size; i++) {
http.addHeader(F(headers[i].name), headers[i].value);
}
return handleUpdate(http, currentVersion, false);
}
// Calling it somewhere:
HttpHeader headers[] = { { String("Accept"), String("application/json") } };
updateheader(client, version, headers, 1);
请注意,这意味着数组数据存在于调用函数的堆栈中,一旦该函数结束,指针就会失效。但是当不使用堆分配时总是这样。