Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

Why can't I use std::string or any other objects from the C++ Standard Library in my sketches for Arduino? #include <string> doesn't throw an error, so it clearly has no trouble finding the libraries, but actually using anything from it does. If I write

std::string str;

I get

'string' is not a member of 'std'

Why?

share|improve this question
1  
I don't know much about Arduino, but might this discussion help you out? stackoverflow.com/questions/9839990/… –  Kurt E. Clothier Apr 29 '13 at 1:05
2  
C++ and class structurers have a home in applications implemented on big memory processors. The AVRs used on the Arduino just do not have a large enough RAM area to even begin to justify C++ classes and coding styles. –  Michael Karas Apr 29 '13 at 2:31
add comment

closed as off topic by Nick Alexeev, Leon Heller, Dave Tweed, placeholder, Anindo Ghosh Apr 29 '13 at 8:26

Questions on Electrical Engineering Stack Exchange are expected to relate to electronics design within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

Most likely your #include command actually includes the .h file extension:

#include <string.h>

Which gets you the C-style string handling functions defined in string.h. If you omit the file extension:

#include <string>

The output is then is:

Fatal error: string: No such file or directory 
compilation terminated.

As expected for the C++ standard strings, which are not easily supported on Arduino (see the StackOverflow post Kurt E. Clothier mentions )

share|improve this answer
    
No, that is not the case. I never write .h when I include a C++ Standard Library header for that exact reason. –  Michael Dorst Apr 29 '13 at 1:23
    
Also, I read that post, and am wondering how one could possibly implement C++ without new or delete operators. That would break constructors and destructors, which stand at the very core of C++ it's self. –  Michael Dorst Apr 29 '13 at 1:25
    
I wonder if you have a string on your Arduino build path containing...??? On your second point, there are discussions on the interwebs about how to implement new and delete: the main problem with this for Arduino is very limited memory (SRAM), and the difficulty of keeping track of non-stack allocated variables, so you wouldn't know when you run out. –  angelatlarge Apr 29 '13 at 1:45
1  
@MichaelDorst, new and delete are entirely orthogonal to constructors and destructors. You can program C++ entirely with statically allocated (global, static, stack) variables. –  microtherion Apr 29 '13 at 3:05
    
Well, the reason I say that new and delete "stand at the very core of C++ it's self", is that they are both central enough to the language that Bjarne Stroustrup saw fit to give them special syntax. So they're definitely not just another library function, they're a core part of the language. <new> is implicitly included in all C++ programs, so to remove it would be to change the language it's self. –  Michael Dorst Apr 29 '13 at 8:46
show 2 more comments

The Arduino supports two kinds of string:

  • C-style character arrays, which the Arduino documentation refers to as strings (lower case 's')
  • String objects, which the the Arduino documentation refers to as Strings (capital 'S')

The later is similar to std::string from the C++ standard library but slightly different. It is an Arduino specific implementation included in the Arduino core library. The Arduino does not use the C++ standard library as it would increase the size of your program too much.

You'll probably find the Arduino String class does what you need. The most significant difference it has from std::string is that is does not support new or delete operators but in most situations this is actually a good thing, new and delete are only used for dynamic (heap based) memory allocations which generally have no place on a 8-bit microcontroller with very limited RAM.

The fact that Arduino does not support new or delete operators does not preclude the use of C++ classes, constructors, etc. It just means that you can not dynamically create and allocate memory to them. This page shows several examples of using the various contructors supported by the Arduino String class. In fact most Arduino librarys are implmented using classes, it's just that when you create instances of these class, they are allocated on the stack or as global variables, rather than dynamically allocated with the new operator.

share|improve this answer
    
Okay, so you're the second person to tell me that new and delete aren't supported on Arduino, so I have this question for you. –  Michael Dorst Apr 29 '13 at 8:42
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.