6

I'm writing a stack class in C++ for an Arduino sketch. I believe it is fully compliant with the AVR (if that's what it's called; I can't remember exactly) compiler; I've used all malloc and free instead of new and delete and so on.

So I have a .h file with my class. I've imported it into the sketch, but when I try to compile, I get these errors:

In file included from sketch_may15a.cpp:1:
/CStack.h:58:18: error: string: No such file or directory
In file included from sketch_may15a.cpp:1:
CStack.h:61: error: variable or field 'Error' declared void
CStack.h:61: error: 'string' was not declared in this scope

And here are the first few lines for my class:

#include <string>
using namespace std;
void Error(string message) {

So the Arduino compiler can't find <string>, and the next few problems seem to relate to it (not sure what variable or field Error declared void means, my error function is just for debugging purposes).

I know Arduino sketches support strings without need for an import, but I'm not sure how that works out with C/C++/.h files. I've tried googling it, but there isn't much documentation.

1
  • 1
    Are you using a C++ compiler? (A C compiler won't find C++ headers under normal circumstances.) Commented May 16, 2012 at 5:24

2 Answers 2

11

Arduino sketches don't support any of the C++ standard library as they are compiled using avr-libc which doesn't support it. However, Arduino does provide the String class which should do what you need.

If you are writing a library you'll also need to #include <Arduino.h> (or #include <Wiring.h> if you are using a pre-1.0 version of the Arduino IDE).

Sign up to request clarification or add additional context in comments.

3 Comments

I changed it all to 'String' and the syntax highlights it, but I still get mostly the same errors: In file included from sketch_may15a.cpp:1: CStack.h:56: error: variable or field 'Error' declared void CStack.h:56: error: 'String' was not declared in this scope Error is declared void Error(String message) {... suppose to be a function
Since you're writing a library you'll probably also need to #include <Arduino.h> (or Wiring.h if you are using a pre-1.0 version of the Arduino IDE).
#include <Arduino.h> fixed the problems. It compiled, so thanks! Now just have to cross fingers and hope it works xD.
1

Using String class in microprocessors is advised against due to heap fragmentation and other problems

The new SafeString library (available from the library manager) solves all those problems and is completely safe and will never cause your sketch to reboot.

see the detailed tutorial at
https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.