我想在现有的库中创建自己的函数,传递一个空数组,以防用户没有任何头。
//.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\2.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).
不允许引用数组。另外,你也不能这样约束内部数组的大小。
也许你想使用一些更现代的工具。
#include <utility>
#include <vector>
t_httpUpdate_return updateheader(WiFiClient& client, const String& url, const String& currentVersion = "",
const std::vector<std::pair<string, string>> &headers = {});
你也可以使用一个数组,但那样你就得把大小传过来,否则你就不知道你有多少个头了!
你写的东西。sizeof(header)
是一个 虫子 - 你得到了一个C风格的数组作为函数参数,数组会衰减为一个指针,并且 sizeof(header)
给你 指针大小 而不是数组的长度!