[Solved] Unable to declare String in Arduino 022

Hi guys

In the past few months, any problem I had was solved almost immediately by looking for answers in this forum. But suddenly I'm confronted with such a bizarre problem that I can't seem to find anyone else experiencing it, and hence, no remedy for it :'(. What am I missing? Please help!

The problem is that I can't declare a String variable in the Arduino IDE.

I've read that from version 19 on, the String type has become a core component of the language, but I keep getting the same message over and over again: "error: 'String' does not name a type"...

To eliminate any code-related reasons, I simply entered the following code into the IDE and tried to compile it:

#include <Wire.h>

String s1="hello";
void setup() {
      Wire.begin();
      Serial.begin(9600);
}

void loop() {  

      Serial.println(s1);
      delay(100);
}

I'm working with the latest IDE version (022) and an Arduino Mega Board.

Any idea?!

Thank you!

I pasted your code into a blank sketch in 0021, and it compiles fine. I'm using Windows 7 64bit.

What OS are you using?

And I did the same using Arduino 022, using windows XP sp3, and selecting the arduino mega1280 board. Complies without errors.

Lefty

Works with my UNO on Windows 7 64bit. IDE is Arduino 22.

Kevin

I'm using Win-XP Professional, I believe it is SP 3.
I started with Arduino 018 about half a year ago, I upgraded to version 21 about 1 month ago, and today I upgraded to version 022 with a hope it might help.

I have a suspicion it has something to do with the list of libraries under the Arduino directory, because I'm using more than just the default ones which came with the installation. This is because there are some additional libraries I must include in the project I'm building.
The libraries directory currently includes the following libraries:
01/07/2011 07:41 PM .
01/07/2011 07:41 PM ..
01/07/2011 05:54 PM ArduinoTestSuite
01/07/2011 05:56 PM ByVacLCD
01/07/2011 05:56 PM DallasTemperature
01/07/2011 05:56 PM DS1307RTC
01/07/2011 05:54 PM EEPROM
01/07/2011 05:54 PM Ethernet
01/07/2011 05:54 PM Firmata
01/07/2011 05:54 PM LiquidCrystal
01/07/2011 05:54 PM Matrix
01/07/2011 05:56 PM OneWire
01/07/2011 05:54 PM SD
01/07/2011 05:54 PM Servo
01/07/2011 05:54 PM SoftwareSerial
01/07/2011 05:54 PM SPI
01/07/2011 05:54 PM Sprite
01/07/2011 05:54 PM Stepper
01/07/2011 05:56 PM Time
01/07/2011 05:56 PM TimeAlarms
01/07/2011 05:54 PM Wire

It's just a wild guess actually...

The project I'm writing spans about 2,500 lines of code, and it works just fine. Just this string issue....!!

What am I missing?... :cry:

The String class is defined in WString.h in the hardware\arduino\cores\arduino directory. You haven't somehow mangled or deleted that file, have you?

The directory exists.
I have not touched it. It is exactly as it was when I downloaded the latest version of the IDE (022)

Here is the content of the directory:

Directory of C:\???\”???—? —??\arduino-0022\hardware\arduino\cores\arduino

01/07/2011  07:52 PM    <DIR>          .
01/07/2011  07:52 PM    <DIR>          ..
12/24/2010  03:48 PM            10,894 binary.h
12/24/2010  03:48 PM             8,691 HardwareSerial.cpp
12/24/2010  03:48 PM             2,259 HardwareSerial.h
12/24/2010  03:48 PM               122 main.cpp
12/24/2010  03:48 PM            12,346 pins_arduino.c
12/24/2010  03:48 PM             3,091 pins_arduino.h
12/24/2010  03:48 PM             4,424 Print.cpp
12/24/2010  03:48 PM             2,056 Print.h
12/24/2010  03:48 PM             1,124 Stream.h
01/07/2011  07:52 PM                 0 tmp.txt
12/24/2010  03:48 PM            14,911 Tone.cpp
12/24/2010  03:48 PM             4,743 WCharacter.h
12/24/2010  03:48 PM                21 WConstants.h
12/24/2010  03:48 PM             6,959 WInterrupts.c
12/24/2010  03:48 PM             8,389 wiring.c
12/24/2010  03:48 PM             3,709 wiring.h
12/24/2010  03:48 PM             6,954 wiring_analog.c
12/24/2010  03:48 PM             4,860 wiring_digital.c
12/24/2010  03:48 PM             1,772 wiring_private.h
12/24/2010  03:48 PM             2,712 wiring_pulse.c
12/24/2010  03:48 PM             1,656 wiring_shift.c
12/24/2010  03:48 PM             1,708 WMath.cpp
12/24/2010  03:48 PM             1,554 WProgram.h
12/24/2010  03:48 PM             9,953 WString.cpp
12/24/2010  03:48 PM             4,304 WString.h
              25 File(s)        119,212 bytes

The file WString.h contains the following text:

/*
  WString.h - String library for Wiring & Arduino
  Copyright (c) 2009-10 Hernando Barragan.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef String_h
#define String_h

//#include "WProgram.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

class String
{
  public:
    // constructors
    String( const char *value = "" );
    String( const String &value );
    String( const char );
    String( const unsigned char );
    String( const int, const int base=10);
    String( const unsigned int, const int base=10 );
    String( const long, const int base=10 );
    String( const unsigned long, const int base=10 );
    ~String() { free(_buffer); _length = _capacity = 0;}     //added _length = _capacity = 0;

    // operators
    const String & operator = ( const String &rhs );
    const String & operator +=( const String &rhs );
    //const String & operator +=( const char );
    int operator ==( const String &rhs ) const;
    int      operator !=( const String &rhs ) const;
    int      operator < ( const String &rhs ) const;
    int      operator > ( const String &rhs ) const;
    int      operator <=( const String &rhs ) const;
    int      operator >=( const String &rhs ) const;
    char operator []( unsigned int index ) const;
    char& operator []( unsigned int index );
    //operator const char *() const { return _buffer; }
    
    // general methods
    char charAt( unsigned int index ) const;
    int      compareTo( const String &anotherString ) const;
    unsigned char endsWith( const String &suffix ) const;
    unsigned char equals( const String &anObject ) const;
    unsigned char equalsIgnoreCase( const String &anotherString ) const;
    int      indexOf( char ch ) const;
    int      indexOf( char ch, unsigned int fromIndex ) const;
    int      indexOf( const String &str ) const;
    int      indexOf( const String &str, unsigned int fromIndex ) const;
    int      lastIndexOf( char ch ) const;
    int      lastIndexOf( char ch, unsigned int fromIndex ) const;
    int      lastIndexOf( const String &str ) const;
    int      lastIndexOf( const String &str, unsigned int fromIndex ) const;
    const unsigned int length( ) const { return _length; }
    void setCharAt(unsigned int index, const char ch);
    unsigned char startsWith( const String &prefix ) const;
    unsigned char startsWith( const String &prefix, unsigned int toffset ) const;
    String substring( unsigned int beginIndex ) const;
    String substring( unsigned int beginIndex, unsigned int endIndex ) const;
    String toLowerCase( ) const;
    String toUpperCase( ) const;
    String trim( ) const;
    void getBytes(unsigned char *buf, unsigned int bufsize);
    void toCharArray(char *buf, unsigned int bufsize);
    long toInt( );
    const String& concat( const String &str );
    String replace( char oldChar, char newChar );
    String replace( const String& match, const String& replace );
    friend String operator + ( String lhs, const String &rhs );

  protected:
    char *_buffer;           // the actual char array
    unsigned int _capacity;  // the array length minus one (for the '\0')
    unsigned int _length;    // the String length (not counting the '\0')

    void getBuffer(unsigned int maxStrLen);

  private:

};

// allocate buffer space
inline void String::getBuffer(unsigned int maxStrLen)
{
  _capacity = maxStrLen;
  _buffer = (char *) malloc(_capacity + 1);
  if (_buffer == NULL) _length = _capacity = 0;
}

inline String operator+( String lhs, const String &rhs )
{
  return lhs += rhs;
}


#endif

The WString.h file should be included automatically. It is for me. What happens if you explicitly include it?

PaulS,
When I add the following line to my code -#include <WString.h>

I get the following message: error: WString.h: No such file or directory

The modified code:

#include <Wire.h>
#include <WString.h>

String s1="hello";
void setup() {
      Wire.begin();
      Serial.begin(9600);
}

void loop() {

      Serial.println(s1);
      delay(100);
}

I also tried restarting, updating Java... :stuck_out_tongue:
Nothing seems to help.

I will proceed with the coding without strings for now, but I'll keep revisiting this post until I solve this issue, so PLEASE don't be deterred to write any idea you may have even if it seems like the post is a bit old. I will update its title as "resolved" once it does...

Also, I posted on another branch of the forum and got requested not to cross-post. I respect this request so I won't try any other branch here. Are you aware of other web-sites in which I can get help?

Thanks again!

One last thing to try:

#include [glow]"[/glow]WString.h[glow]"[/glow]

It sounds like the IDE isn't installed right or the wrong version is running. Hold down Shift while pressing the verify button and post the output here, that might give us a clue to what's going on.

Also, when you select Help->About Arduino, what version appears in the box?

Stimmer, if you ever plan to run for presidency somewhere on the globe, please consider me as your most loyal supporter.

No more help is needed! Once I followed Stimmer's instructions I noticed a strange link to the old Arduino018 folder. I relocated it to somewhere else on my disk, and this caused the Arduino to state that its scratch-book folder does not exist anymore, hence it will create a new one, and than it will stop referring to itself in a third person (I'm not joking - that was the statement... ::)).

I than loaded the code again, and voilla, it compiles!

3 days I wasted. 3 days... >:(

Long live Stimmer. Long live the king

Thank you all for your help!