'String' has not been declared error

Hello,

I have a problem with String variable during the compilation. I get the following error for every String:
“In file included from sketch\pump.cpp:1:0:
pump.h:16: error: ‘String’ has not been declared
Pump(int ID, String text, int DurMaxi, int Press, int TestDur, Pump * prec);”

I always have errors when i used String variable except in the Main file.
I have tried to put all the code in only one file and I can compile without any error.

Do I need to add something ?
I have searched on Google without finding any clue.
It is probably very simple but it’s been a long time since the last time, i used C++.
Thank you for your help.

Main

// Libraries
 
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <String>

#include "pump.cpp" 


#define USE_SERIAL Serial
const char *  SERVERADR = "http://192.168.1.4/arduino/";

ESP8266WiFiMulti WiFiMulti;
Pump test;


HTTPClient http;
 
void setup() {
 
  Serial.begin(115200);      // Start Serial 
  USE_SERIAL.begin(115200);
  // USE_SERIAL.setDebugOutput(true);
 
  USE_SERIAL.println();

  USE_SERIAL.printf("[SETUP] WAIT %d...\n");
  for(uint8_t t = 8; t > 0; t--) {
      USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
      USE_SERIAL.flush();
      delay(1000);
  }
 
  WiFiMulti.addAP("ChinaNet-J4S5", "NA");
  WiFiMulti.addAP("Router2-SSI", "Router2-password");
  
  http.setReuse(true);

  if((WiFiMulti.run() == WL_CONNECTED)) {

    http.begin(String(SERVERADR)+"getpumpdata.php"); //HTTP
    // start connection and send HTTP header
    int httpCode = http.GET();
    
    // httpCode will be negative on error
      if(httpCode > 0) {
          // HTTP header has been send and Server response header has been handled
          USE_SERIAL.printf("[HTTP] GET Pump Data... code: %d\n", httpCode);
   
          // file found at server
          if(httpCode == HTTP_CODE_OK) {
              String payload = http.getString();
              USE_SERIAL.println(payload);
          }
      } else {
          USE_SERIAL.printf("[HTTP] GET... failed receive pump data, error: %s\n", http.errorToString(httpCode).c_str());
      }
  }
  else {
    USE_SERIAL.printf("Connexion wifi problem\n");
  }
} 
 
void loop() 
{
  // wait for WiFi connection
  if((WiFiMulti.run() == WL_CONNECTED)) {
    //Serial.println(WiFi.localIP());
   
   
    USE_SERIAL.print("[HTTP] begin...\n");
    // configure traged server and url
    char  Data[30];
    CreateDataString(Data, 3, 5, 8, 321, 1);
  
    http.begin(String(SERVERADR)+"serverarduino2.php?Data="+String(Data)); //HTTP
  
    USE_SERIAL.print("[HTTP] GET...\n");
    // start connection and send HTTP header
    int httpCode = http.GET();
    
      // httpCode will be negative on error
      if(httpCode > 0) {
          // HTTP header has been send and Server response header has been handled
          USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
   
          // file found at server
          if(httpCode == HTTP_CODE_OK) {
              String payload = http.getString();
              USE_SERIAL.println(payload);
          }
      } else {
          USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }
   
      http.end();
      delay(100);   // wait a minute
  }
 
}

void CreateDataString(char Data[30],int user, int TestbBench, int PumpType, int Duration, int valid)
{  
  sprintf(Data, "%.2d", user);
  
  sprintf(Data, "%s%.2d",Data, TestbBench);
  
  sprintf(Data, "%s%.2d",Data, PumpType);
  
  sprintf(Data, "%s%.4d",Data, Duration);
  
  sprintf(Data, "%s%.2d",Data, valid);
  
 
  unsigned int checksum =crc32b(Data);
  
  sprintf(Data, "%s%u",Data, checksum);

}


 unsigned int crc32b( char *message) {
   int i, j;
   unsigned int byte, crc, mask;

   i = 0;
   crc = 0xFFFFFFFF;
   while (message[i] != 0) {
      byte = message[i];            // Get next byte.
      crc = crc ^ byte;
      for (j = 7; j >= 0; j--) {    // Do eight times.
         mask = -(crc & 1);
         crc = (crc >> 1) ^ (0xEDB88320 & mask);
      }
      i = i + 1;
   }
   return ~crc;
}

Pump.cpp

#include "pump.h"
Pump::Pump()
{  
  ID = 0;

  DurationMaxi = 0;
  Pressure= 0;
  TestDuration= 0;
  Next = NULL;
}

Pump::Pump(int IDNb, String text, int DurMaxi, int Press, int TestDur, Pump * prec)
{
  ID = IDNb;
  text.toCharArray(Name,20);
  DurationMaxi = DurMaxi;
  Pressure= Press;
  TestDuration= TestDur;
  //Next = NULL;
  prec->SetNext(this);
}

Pump::Pump(String text, Pump * prec)
{
  const char separator = '|';
  int temp1, temp2;
  temp1 = 0;
  temp2 = text.indexOf(separator, temp1+1);
  ID =  text.substring(temp1, temp2 - temp1).toInt();
  
  temp1 = temp2+1;
  temp2 = text.indexOf(separator, temp1);
  text.substring(temp1, temp2 - temp1).toCharArray(Name,20);
  
  temp1 = temp2+1;
  temp2 = text.indexOf(separator, temp1);
  DurationMaxi = text.substring(temp1, temp2 - temp1).toInt();
  
  temp1 = temp2+1;
  temp2 = text.indexOf(separator, temp1);
  Pressure= text.substring(temp1, temp2 - temp1).toInt();
  
  temp1 = temp2+1;
  temp2 = text.indexOf(separator, temp1);
  TestDuration= text.substring(temp1, temp2 - temp1).toInt();
  
  prec->SetNext(this);
}

void Pump::SetNext(Pump * Nxt)
{
  Next = Nxt;  
}

int Pump::GetDurationMaxi()
{
  return DurationMaxi;  
}

int Pump::GetPressure()
{
  return Pressure;  
}

int Pump::GetTestDuration()
{
  return TestDuration;  
}

char* Pump::GetName()
{
  return Name;  
}

Pump.h

#ifndef DEF_PUMP
#define DEF_PUMP

class Pump
{
  private:
  int ID;
  char Name[20];
  int DurationMaxi;
  int Pressure;
  int TestDuration;
  Pump * Next;

  public:
  Pump();
  Pump(int ID, String text, int DurMaxi, int Press, int TestDur, Pump * prec);
  Pump(String text, Pump * prec);
  int GetDurationMaxi();
  void SetNext(Pump * prec);
  int GetPressure();
  int GetTestDuration();
  char* GetName();
};

#endif
#include <String>

1 - do you really need to #include the String library ?
2 - if you do then why not do it properly by #including String.h ?

#include "pump.cpp"It is more usual to #include the .h file

1- the String library is normally not necessary but i tried different things to solve this issue.

2- Yes, I agree with you. I modified the include but I still have the same problem.

Try a really simple program.

Don't explicitly #include any libraries
Declare and initialise a String in setup()
print the String in setup()

Does it work ?

Whether it works or not consider using strings (lowercase s - zero terminated arrays of chars) rather than memory gobling Strings (uppercase S - objects created using the String library)

Whenever I see something like this:

  • text.toCharArray(Name,20);*

and given all the bad press around the String class, I wonder why you're even bothering to use a String in the first place. Just use a char array and be done with it.

Hello,

I have made a simple script and i have just include a cpp file where i declare a String variable.

I have another error but it is still linked to the string library:

"test.cpp:1: error: 'String' does not name a type

String teststring = "ABCDE";"

Main

#include "test.cpp"

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);


  // send an intro:
  Serial.println("\n\nString  substring():");
  Serial.println();
}

void loop() {
  // Set up a String:
  String stringOne = "text/html";
  Serial.println(stringOne);


}

test.cpp

String teststring = "ABCDE";

econjack:
Whenever I see something like this:

  • text.toCharArray(Name,20);*

and given all the bad press around the String class, I wonder why you're even bothering to use a String in the first place. Just use a char array and be done with it.

This part of the code is only temporary. I want to check my code at each step. I haven't used c++ for 5 years so i need to try different solutions otherwise i will lose too much time.
Thank you.

Regardless of whether or not you include the cpp file, it will be compiled separately.

So, look at JUST your cpp file. How is the compiler supposed to know what a String is?

If you #include Arduino.h in your header or source files, the problem will go away. That file is automatically added to your .ino file.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

With cstrings, i have the feeling that i need re-invent the wheel. Simple function aren’t implemented.
I will try to not use it even if I need to lose time at the beginning.

Thank you.

MoFanFr:
Simple function aren't implemented.

Sure they are. Look at all those useful functions at the link Robin2 provided. You will find more here:
http://www.cplusplus.com/reference/cstdlib/

MoFanFr:
With cstrings, i have the feeling that i need re-invent the wheel. Simple function aren't implemented.
I will try to not use it even if I need to lose time at the beginning.

It is true that cstrings are not as easy to use as the String class - presumably that is why the String class was invented. However the memory limitations of the Arduino take precedence over convenience.

...R