I am writing a small library to move some code out of the arduino sketch and make it reusable. Problem is that it does not compile since "String does not name a type".
This is my code (reduced):
myESP.h:
#ifndef MYESP_H
#define MYESP_H
#include "Arduino.h"
#include <String>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
class myESP {
private:
const char* _ssid;
const char* _password;
const char* _host;
public:
myESP();
myESP(char * ssid, char * pwd, char * host);
String macToStr(const uint8_t* mac);
String doGet(String data, String sensor, int duration);
};
#endif
myESP.cpp:
#include <String>
#include "Arduino.h"
#include "myESP.h"
myESP::myESP() {}
myESP::myESP(char * ssid, char * pwd, char * host) {
_host = host;
_password = pwd;
_ssid = ssid;
}
myESP::String macToStr(const uint8_t* mac) {
}
myESP::String doGet(String data, String sensor, int duration) {
}
When i try to compile it, I get the following:
/Users/lbedogni/Documents/Arduino/libraries/myESP/myESP.cpp:14:1: error: 'String' in 'class myESP' does not name a type myESP::String macToStr(const uint8_t* mac) {
/Users/lbedogni/Documents/Arduino/libraries/myESP/myESP.cpp:22:1: error: 'String' in 'class myESP' does not name a type myESP::String doGet(String data, String sensor, int duration) {
I have tried to change the order of the imports, change libraries, but still it does not compile.
Any idea?
1 Answer 1
Your syntax is wrong in myESP.cpp. Change this:
myESP::String macToStr(const uint8_t* mac) {
}
myESP::String doGet(String data, String sensor, int duration) {
}
to this:
String myESP::macToStr(const uint8_t* mac) {
}
String myESP::doGet(String data, String sensor, int duration) {
}
The return type is String
and the function implementation should be prefixed with the class name myESP::
. Obviously, you also need to fill in the body of the implementation or you'll get more errors because you don't actually return anything.
#include <String>
- the IDE automatically includes theString
type so you don't need this include.