Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unknown type name 'String'" when creating custom Arduino Libraries

I have a few logging functions that I commonly use for different Arduino programs. Since I use them so much, I decided to try to make a custom library for them. Unfortunately, the compiler crashes at the header file with the error:

unknown type name 'String'

I'm a bit confused as to why this is happening because I am including the standard Arduino libraries (which I believe should contain the String class) at the top of my header. Here's the whole thing:

#ifndef logging_h
#define logging_h

#include "Arduino.h"

void logEvent(String msg);
void debugOut(String msg);
void errOut(String err);
void document(String parameter, float value);

#endif 

I reinstalled the Arduino IDE (1.0.5) so I think I should have the most recent standard library. If anyone has some suggestions I would really appreciate it.

like image 337
sdrendall Avatar asked Mar 25 '14 14:03

sdrendall


1 Answers

(This answer is based on our discussion in comments.)

The problem was that the source file for your library was named *.c. That caused the compiler to treat it as C code instead of C++, which means it couldn't handle classes/objects (such as String).

Naming the file *.cpp instead lets the compiler treat it correctly as C++ code.

like image 115
Peter Bloomfield Avatar answered Nov 15 '22 11:11

Peter Bloomfield