# 'String' has not been declared error

**URL:** <https://forum.arduino.cc/t/string-has-not-been-declared-error/518538>\
**Category:** Programming\
**Created:** [April 5, 2018, 3:02pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538 "2018-04-05T15:02:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoFanFr](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoFanFr](https://forum.arduino.cc/u/MoFanFr)\
**Post date:** [April 5, 2018, 3:02pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/1 "2018-04-05T15:02:45Z")

</div>

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

```cpp
// 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

```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

```cpp
#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

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 5, 2018, 3:06pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/2 "2018-04-05T15:06:52Z")

</div>

```cpp
#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

---

<div class="post-metadata">

**Author:** ![MoFanFr](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoFanFr](https://forum.arduino.cc/u/MoFanFr)\
**Post date:** [April 5, 2018, 3:26pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/3 "2018-04-05T15:26:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 5, 2018, 3:40pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/4 "2018-04-05T15:40:59Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![econjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/econjack/32/1105255_2.png) [@econjack](https://forum.arduino.cc/u/econjack)\
**Post date:** [April 5, 2018, 3:45pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/5 "2018-04-05T15:45:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MoFanFr](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoFanFr](https://forum.arduino.cc/u/MoFanFr)\
**Post date:** [April 5, 2018, 4:00pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/6 "2018-04-05T16:00:24Z")

</div>

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

```cpp
#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

```cpp
String teststring = "ABCDE";

```

---

<div class="post-metadata">

**Author:** ![MoFanFr](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoFanFr](https://forum.arduino.cc/u/MoFanFr)\
**Post date:** [April 5, 2018, 4:20pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/7 "2018-04-05T16:20:34Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 5, 2018, 4:28pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/8 "2018-04-05T16:28:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [April 5, 2018, 5:01pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/9 "2018-04-05T17:01:46Z")

</div>

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](http://www.cplusplus.com/reference/cstring/) - char arrays terminated with 0.

...R

---

<div class="post-metadata">

**Author:** ![MoFanFr](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoFanFr](https://forum.arduino.cc/u/MoFanFr)\
**Post date:** [April 6, 2018, 5:01am UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/10 "2018-04-06T05:01:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [April 6, 2018, 7:31am UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/11 "2018-04-06T07:31:03Z")

</div>

> 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/](http://www.cplusplus.com/reference/cstdlib/)

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [April 6, 2018, 8:56am UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/12 "2018-04-06T08:56:01Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:06pm UTC](https://forum.arduino.cc/t/string-has-not-been-declared-error/518538/13 "2021-05-05T23:06:56Z")

</div>

Closed on May 5, 2021, 11:06pm UTC


