Oop 无法实例化类 我是C++初学者。因此,请容忍我:
我尝试为温度传感器编写代码,该传感器应将值发布到API 我无法实例化我的类Oop 无法实例化类 我是C++初学者。因此,请容忍我:,oop,arduino,esp32,arduino-c++,Oop,Arduino,Esp32,Arduino C++,我尝试为温度传感器编写代码,该传感器应将值发布到API 我无法实例化我的类ApiClient。 我总是会遇到以下错误: IDE:在没有适当运算符()或转换函数的情况下,调用类类型的对象以指向函数类型的指针 IDE:重载函数“ApiClient::ApiClient”的实例与指定类型不匹配 编译器:对“(ApiClient)(字符串&)”的调用不匹配 我的代码如下所示(为便于阅读,略微精简): main.cpp #include <ApiClient.h> ApiClient ap
ApiClient
。
我总是会遇到以下错误:
- IDE:在没有适当运算符()或转换函数的情况下,调用类类型的对象以指向函数类型的指针
- IDE:重载函数“ApiClient::ApiClient”的实例与指定类型不匹配
- 编译器:对“(ApiClient)(字符串&)”的调用不匹配
#include <ApiClient.h>
ApiClient api;
void setup()
{
Serial.begin(115200);
String apiUrl = "https://myapi.com/api";
api(apiUrl); // ERROR: raises call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type
}
void loop()
{
String temp = "49";
api.sendValue("temperature", temp);
}
#include <ApiClient.h>
String _apiUrl;
HTTPClient http;
ApiClient::ApiClient(String apiUrl) // IDE says: "no instance of overloaded function "ApiClient::ApiClient" matches the specified type"
{
this->_apiUrl = apiUrl;
// some stuff to establish a connection
}
#包括
ApiClient api;
无效设置()
{
序列号开始(115200);
字符串apiUrl=”https://myapi.com/api";
api(apiUrl);//错误:在没有适当运算符()或转换函数的情况下,引发类类型的对象对函数类型指针的调用
}
void循环()
{
字符串temp=“49”;
api.sendValue(“温度”,温度);
}
ApiClient.h
#ifndef WebClient_h
#define WebClient_h
#include <Arduino.h>
#include <WiFi.h>
#include <esp_wps.h>
#include <HTTPClient.h>
class ApiClient
{
public:
ApiClient(); // this is only added for testing
ApiClient(String apiUrl); // it should always instanciate with url
void sendValue(String key, String value);
String _apiUrl;
private:
};
#endif
\ifndef网络客户端
#定义网络客户端
#包括
#包括
#包括
#包括
类ApiClient
{
公众:
ApiClient();//这只是为了测试而添加的
ApiClient(字符串APIRL);//它应该始终与url实例关联
void sendValue(字符串键、字符串值);
字符串apiUrl;
私人:
};
#恩迪夫
ApiClient.cpp
#include <ApiClient.h>
ApiClient api;
void setup()
{
Serial.begin(115200);
String apiUrl = "https://myapi.com/api";
api(apiUrl); // ERROR: raises call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type
}
void loop()
{
String temp = "49";
api.sendValue("temperature", temp);
}
#include <ApiClient.h>
String _apiUrl;
HTTPClient http;
ApiClient::ApiClient(String apiUrl) // IDE says: "no instance of overloaded function "ApiClient::ApiClient" matches the specified type"
{
this->_apiUrl = apiUrl;
// some stuff to establish a connection
}
#包括
字符串apiUrl;
HTTPClient-http;
ApiClient::ApiClient(字符串apiUrl)//IDE说:“重载函数的任何实例”ApiClient::ApiClient“都与指定的类型不匹配”
{
此->\u apirl=apirl;
//一些东西来建立联系
}
您正试图对已构造的对象调用构造函数
此线路出现故障:
api(apiUrl);
您尝试的调用失败,因为编译器正在查找以下函数之一:
return_type APIClient::operator()(const String&);
return_type APIClient::operator()(String);
return_type APIClient::operator()(String&);
如果希望调用构造函数,则语句应该是声明
ApiClient api(apiUrl);
// or better in c++11:
APIClient api{ apiUrl };
或者,您可以在ApicClient中创建一个初始值设定项,该初始值设定项将单个字符串作为参数,如下所示:
class ApiClient
{
public:
ApiClient() { ...}
ApiClient(const String& s) { Initialize(s); }
Initialize(const String& s) { do the work }
...
};
在类中提供一步和两步初始化API时要小心。在使用新字符串重新初始化时,您需要一个适当的重置函数以避免混淆。您有一个全局变量,它在
main
之前构造,但您希望从main
参数构造它。看到问题了吗?您有一个设计问题,因为api
在循环中使用。重写一切,从责任开始,制定一个对象层次结构。为什么您需要一个统一的ApiClient来进行测试?这个错误来自DaboIT构造函数的存在。@ MatthieuBrucher,我仍然用C++学习(NOOB)。因此,我只尝试重构这个示例(),以保持main.cpp精简@米夏尔·罗伊。我认为apiclientapi
仅声明api,而ApiClient api(apiEndpoint)代码>实例化它。因此,全局声明它,以便能够在main(初始化)中使用它,并在需要时在循环中调用api.sendvalue(k,v)
。但是我似乎是理解C的第一个开始。如果你正在学习C++,不要尝试复杂的事件和其他的东西。去做一些简单的事情。