Hi Everyone,
I am trying to use WString Library on Arduino 1.0.3. But following errors are encountered whenever I compile the code.
I would be very thankful if someone can provide solution to this issue asap.
//////////////////////////////////////////////////////////////////////////////////
StringContains.pde: In function 'void setup()':
StringContains:27: error: 'class String' has no member named 'version'
StringContains.pde: In function 'void loop()':
StringContains:38: error: 'class String' has no member named 'contains'
StringContains.pde: In function 'void getIncomingChars()':
StringContains:55: error: 'class String' has no member named 'append'
StringContains:59: error: invalid conversion from 'char' to 'const char*'
StringContains:59: error: initializing argument 1 of 'String& String::operator=(const char*)'
///////////////////////////////////////////////////////////////////////////////////
Example code provided by WString library was used.
#include <WString.h> // include the String library
#define maxLength 30
String inString = String(maxLength); // allocate a new String
void setup() {
// open the serial port:
Serial.begin(9600);
// Say hello:
Serial.print("String Library version: ");
Serial.println(inString.version());
}
void loop () {
// See if there's incoming serial data:
if(Serial.available() > 0) {
getIncomingChars();
// print the string
Serial.println(inString);
}
// look for the otter:
if (inString.contains("otter")) {
Serial.println("Look! An otter in the String!");
// print the string
Serial.println(inString);
// empty the string:
inString = "";
Serial.println("now it's gone.");
}
}
void getIncomingChars() {
// read the incoming data as a char:
char inChar = Serial.read();
// if you're not at the end of the string, append
// the incoming character:
if (inString.length() < maxLength) {
inString.append(inChar);
}
else {
// empty the string by setting it equal to the inoming char:
inString = inChar;
}
}
#define maxLength 30
String inString = String(maxLength); // allocate a new String
void setup() {
// open the serial port:
Serial.begin(9600);
// Say hello:
Serial.print("String Library version: ");
// Serial.println(inString.version());
}
void loop () {
// See if there's incoming serial data:
if(Serial.available() > 0) {
getIncomingChars();
// print the string
Serial.println(inString);
}
// look for the otter:
if (inString.indexOf("otter")) {
Serial.println("Look! An otter in the String!");
// print the string
Serial.println(inString);
// empty the string:
inString = "";
Serial.println("now it's gone.");
}
}
void getIncomingChars() {
// read the incoming data as a char:
char inChar = Serial.read();
// if you're not at the end of the string, append
// the incoming character:
if (inString.length() < maxLength) {
inString += (inChar);
}
else {
// empty the string by setting it equal to the inoming char:
inString = String (inChar);
}
}
Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.
In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.
I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.
Hi Mr. Gammon,
Thanks for the prompt reply. The code you provided is compiling and generates no errors. However as you have mentioned that the String library does have bugs in version 1.0.3. Are these bugs removed in version 1.0.4 ?.