Why EthernetClient does not name a type?

Hi, all,

I am using the Ethernet library to connect the board to the web server, but when I try to compile the following code, it throws out " ‘EthernetClient’ does not name a type":


#include <SPI.h>
#include <Ethernet.h>

class foo
  EthernetClient cli;

project file

#include "foo.h"
//#include <SPI.h>
//#include <Ethernet.h>

foo f;
void setup()

void loop()

It seems both the Ethernet.h and SPI.h files should be re-imported in the project file to get it work. Both of them were imported in foo.h and the project file has included the foo.h already, why does the compiler throw out the exception, even though there were no instances of EthernetClient declared at all ?



This is caused by the mucking about that the Arduino does before compiling your code. If it used straight forward C++ you wouldn't have irrational behaviour like this.

What happens is the Arduino IDE uses the set of #include files in your .ino file to determine what libraries you are using, and then makes the corresponding source and header files available to the compiler (by copying the files into the compilation directory and setting include file paths and so on). If ANY of your code uses a library indirectly or directly, you must include the libary's header file in your main .ino file. This also holds for libraries which require other libraries - even though you may never access the lower level libraries in your code, you MUST include the corresponding header in your sketch .ino file in order for the sketch to build successfully.