error: 'Key' does not name a type

This one has me stumped. I appreciate suggestion on how to trouble shoot this compile error.
What could be causing this error?

Compiler’s verbose output:

Arduino: 1.0.6 + Td: 1.20 (Windows 7), Board: "Teensy 2.0"
C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega32u4 -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -DTEENSYDUINO=120 -felide-constructors -std=c++0x -DUSB_HID -DLAYOUT_US_ENGLISH -IC:\Program Files (x86)\Arduino\hardware\teensy\cores\teensy -IC:\Program Files (x86)\Arduino\libraries\Wire -IC:\Users\wolf\Documents\Arduino\libraries\keybrd C:\Users\wolf\AppData\Local\Temp\build3761118246927349274.tmp\key_error.cpp -o C:\Users\wolf\AppData\Local\Temp\build3761118246927349274.tmp\key_error.cpp.o 

key_error:4: error: 'Key' does not name a type

BaseCodeKey.h

#ifndef BASECODEKEY_H
#define BASECODEKEY_H

class BaseCodeKey
{
    public:
        virtual void press()=0;
        virtual void release()=0;
};
#endif

Key.h

#ifndef KEY_H
#define KEY_H
#include <Arduino.h>
#include "BaseCodeKey.h"

class Key : public virtual BaseCodeKey
{
    void press() {}
    void release() {}

};
#endif

key_error.ino

#include <Wire.h>
#include <Key.h>

Key k;      //error: 'Key' does not name a type

void setup() { }

void loop() { }

There are other files and classes in the same library folder as Key, but I don’t think they are relevant.
The entire folder with Key.h is attached.
I am compiling on Arduino 1.0.6, Teensyduino 1.20.

Thank you.

keybrd.zip (132 KB)

I am unfamiliar with this syntax:

virtual void press()=0;

If there’s a problem with it, then this means that Key won’t compile (because the base class isn’t there) and you will get the error you are getting. … but when I pull your code into a single sketch, then it all compiles so that isn’t a problem.

Aha! Gottit!

#include <Key.h>

If you use angle brackets, the preprocessor only looks in the standard library directories. If you want it to find key.h in the current directory, use quotes:

#include "Key.h"

Karma me! :slight_smile:

Hi Paul. I fixed the include to quotes as you suggested, but still get the same error.

key_error.ino

#include <Wire.h>
#include "Key.h"

Key k;      //error: 'Key' does not name a type

void setup() { }

void loop() { }

compiler’s verbose output:

Arduino: 1.0.6 + Td: 1.20 (Windows 7), Board: "Teensy 2.0"
C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega32u4 -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -DTEENSYDUINO=120 -felide-constructors -std=c++0x -DUSB_HID -DLAYOUT_US_ENGLISH -IC:\Program Files (x86)\Arduino\hardware\teensy\cores\teensy -IC:\Program Files (x86)\Arduino\libraries\Wire -IC:\Users\wolf\Documents\Arduino\libraries\keybrd C:\Users\wolf\AppData\Local\Temp\build823838601659666300.tmp\key_error.cpp -o C:\Users\wolf\AppData\Local\Temp\build823838601659666300.tmp\key_error.cpp.o 

key_error:4: error: 'Key' does not name a type

Maybe something is wrong in the library folder that Key is in?

This is probably obvious, but did you close the IDE after you loaded the Key library into the libraries subdirectory and then reopen it?

Hi econjack,

Yes I closed the IDE, and also deleted the AppData\Local\Temp\build823838601659666300.tmp folder.

One thing that you haven't told us is where the Key.h and Key.cpp files (and the BaseCodeKey files) are. Are they in the same folder as the sketch, or are they in the libraries folder? Screenshots showing where the files really are, so we can replicate (or correct) your setup will save a lot of guessing.

Hi PaulS,

The Key.h and BaseCodeKey.h files are in an Arduino libraries folder named keybrd.
There are no Key.cpp or BaseCodeKey.cpp files.
The key_error.ino sketch is outside the libaray.

Here is a screenshot of the keybrd folder:
Key.png

Compiled OK for me for UNO on 1.6.4. I put the original texts (including the ‘<’ and ‘>’) in the libraries folder:
file:///Users/john/Documents/Arduino/libraries/keybrd/BaseCodeKey.h
file:///Users/john/Documents/Arduino/libraries/keybrd/Key.h
file:///Users/john/Documents/Arduino/libraries/keybrd/examples/key_error/key_error.ino

I then loaded File->Examples->keybrd->key_error

Build options changed, rebuilding all

Sketch uses 1,560 bytes (4%) of program storage space. Maximum is 32,256 bytes.
Global variables use 236 bytes (11%) of dynamic memory, leaving 1,812 bytes for local variables. Maximum is 2,048 bytes.

Perhaps it is time to update to 1.6.4?

wolfv:
The Key.h and BaseCodeKey.h files are in an Arduino libraries folder named keybrd.

Then try

#include <keybrd/Key.h>

Good idea PaulMurrayCbr. I tried it, and unfortunately it still gets the same compile error.

Something odd is going on.

With the minimal files needed, it built for me under 1.0.5 and 1.6.3
It will not, however build with all the files as exacted from the zip as keyboard.h is missing and the IDE will attempt to compile all the modules under the keybrd directory since it thinks that it is a "library" based on the include file in the sketch.

The IDE build methodology is pretty goofy and wimpy and in some cases does some dumb things or even incorrect things.

In order to let the user write incomplete (bad) code, the IDE will scan the .ino file and attempt to convert it to proper C++ code before it hands the .cpp file off to the compiler.
The main thing it does is insert another include and then insert function prototypes.
It also will scan the users sketch and look for header files that it thinks belong to other arduino "libraries".

By "arduino tradition" arduino "libraries" and their main header file have the same name.
While the IDE does not enforce this, it is something to keep in mind and different versions of the IDE handle the libraries, their headers, and how to deal with header file collisions in arduino "libraries" differently.
Basically if there is a header file collision, you are SOL since the IDE can potentially grab the wrong header and in the newer version of the IDE, the "fix" for header file collisions is guaranteed to not do what, if the "library" directory name is not the same basename as the header file.
The root cause is that IDE depends on an include path that it builds up for the compiler so if there is a collision with a directory earlier in the include path, the header will be fetched from the first directory in the include path vs another directory that the IDE "prefered" for that header that was also added to the include path.
i.e. different parts of the IDE are no in sync with each other over which header file will end up being used by the compiler when there are collisions.

Also keep in mind that the IDE build methodology is so simplistic that it can have issues if you try to create a library that sketches can directly include different headers for different classes.
The IDE assumed a very simplistic system of sketches including only a single header file for a given library.
Go beyond that and you run the risk of issues.

The IDE seems to be seeing your Key.h file down in your keybrd libraries directory since it gave this to the compiler:

-IC:\Users\wolf\Documents\Arduino\libraries\keybrd

What seems odd is that "Key" class is not known.

First thing I'd do is go look at the actual .ccp file down in the tmp directory that is being compiled.
I've seen the IDE make a mess of the user's sketch file by incorrectly changing when it inserts its includes and prototypes when creating the .cpp file from the .ino file.

If that looks OK, then the other thing I'd look for is another header file named "Key.h".
I didn't try build this on a 1.0.6 IDE or one with the Teensy stuff installed but I know that 1.0.6 has the new code that tries to deal with library header file collisions. Paul and I had some heated discussion over how to handle this and I still will say that I believe that the way the IDE is handling this is wrong and under certain conditions is guaranteed to silently create strange issues like the one you are seeing.

I have run into this issue many times as it always gets tripped up with the way I'm doing library regression testing for my own libraries.
It ends up telling the sketch to use one version of the "library" but then compiles and links in another.
So the includes for the sketch are not coming from the "library" that is being built.
And this is what Paul and I were arguing about. I wanted to the IDE to guarantee that his would never happen.
The Arduino IDE answer was that in this specific condition, behavior is undefined. (They punted)
I thought that was unacceptable and believe that while the IDE might not pick the "library" you wanted under certain conditions, it should at least always guarantee that everything is consistent for a given arduino "library".
i.e. the arduino "library" being compiled,built, and linked in is the same library the sketch is using for its header files.
(The real answer is to change the IDE build methodology but that is a much bigger change)

Also, keep in mind that the the IDE will attempt to compile every single .cpp or .c file down in an arduino "library" directory when it thinks that "library" is being used.

bperrybap:
If that looks OK, then the other thing I'd look for is another header file named "Key.h".

wolfv: if you have installed the Keypad library it uses a header file called Key.h

Hi bperrybap,
I am still digesting what you wrote.

Hi mstanley,
I do not have the Keypad library installed.

Hi johnwasser,
Thank you for testing my code. I upgraded to Arduino 1.6.4 today (see below).

Thank you all for your suggestions.

I upgraded from Aruduino 1.0.6 to 1.6.4 today.

Then I tested this simple blinky.ino sketch:

#include <Keyboard.h>

const int ledPin = 11;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(900);

  Keyboard.press(KEY_A);
  delay(100);
  Keyboard.release(KEY_A);
}

Error compiling:

Arduino: 1.6.4 (Windows 7), TD: 1.23, Board: "Teensy 2.0, Keyboard + Mouse + Joystick, 16 MHz, US English"

Build options changed, rebuilding all



C:\Program Files (x86)\arduino-1.6.4/hardware/tools/avrteensy/bin/avr-g++ -c -Os -g -Wall -ffunction-sections -fdata-sections -MMD -fno-exceptions -felide-constructors -std=c++0x -mmcu=atmega32u4 -DTEENSYDUINO=123 -DARDUINO=10604 -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DUSB_HID -DLAYOUT_US_ENGLISH -IC:\Program Files (x86)\arduino-1.6.4\hardware\teensy\avr\cores\teensy C:\Users\wolf\AppData\Local\Temp\build6153763175815663552.tmp\blinky.cpp -o C:\Users\wolf\AppData\Local\Temp\build6153763175815663552.tmp\blinky.cpp.o 

blinky.ino:8:22: fatal error: Keyboard.h: No such file or directory
compilation terminated.
Error compiling.

When I remove “#include <Keyboard.h>” from the blinky.ino sketch, it compiles and runs fine.
So “Keyboard.press(KEY_A);” runs if “#include <Keyboard.h>” is omitted from the sketch.
Is that how the new Arduino IDE is supposed to work?

bperrybap:
The IDE assumed a very simplistic system of sketches including only a single header file for a given library.

Whoa, that's not going to work for my keybrd library. Thanks for the warning.
But I love using the Aruino Keyboard and Wire libraries.

I would appreciate it if you could point me to some information on using Arduino Keyboard and Wire libraries with out the Arduino IDE.
I used the Make utility on C++ programs, but have not used Make with Ardunio libraries.

So far I have found two resources for using Makefiles rather than Arduino IDE:

  1. "The GCC Compiler and Tools" at Using avr-gcc with the Teensy USB development board
    Will pjrc's package be able to use Aruino Keyboard and Wire libraries?

  2. This is 4 years old: https://pragprog.com/magazines/2011-04/advanced-arduino-hacking
    Is it still relevant to the new Arduino?

bperrybap:
the the IDE will attempt to compile every single .cpp or .c file down in an arduino "library" directory when it thinks that "library" is being used.

Compiling unused files wouldn't be so bad. But do the unused object files get linked and bloat the executable hex file?

But do the unused object files get linked and bloat the executable hex file?

No. The linker only links in code that is needed.

Thanks PaulS.

I just found the same answer at http://www.arduino.cc/en/Hacking/BuildProcess:

These .o files are then linked together into a static library and the main sketch file is linked against this library. Only the parts of the library needed for your sketch are included in the final .hex file, reducing the size of most sketches.

Now I am thinking there is something wrong with my newly installed Arduino 1.6.4 IDE because this simple program is not compiling:

key_error.ino

#include "Key_Null.h"
#include "Key.h"

Key_Null k;

void setup() { }
void loop() { }
#ifndef KEY_NULL_H
#define KEY_NULL_H
#include <Arduino.h>
#include "Key.h"

class Key_Null : public Key //compiles if inheritance is commented
{                           //error: expected class-name before '{' token
    public:
        void press() {};
};
#endif
#ifndef KEY_H
#define KEY_H
#include <Arduino.h>

class Key
{
    virtual void press()=0;
};
#endif

error message:

Arduino: 1.6.4 (Windows 7), TD: 1.23, Board: "Teensy 2.0, Keyboard + Mouse + Joystick, 16 MHz, US English"

Build options changed, rebuilding all



C:\Program Files (x86)\arduino-1.6.4/hardware/tools/avrteensy/bin/avr-g++ -c -Os -g -Wall -ffunction-sections -fdata-sections -MMD -fno-exceptions -felide-constructors -std=c++0x -mmcu=atmega32u4 -DTEENSYDUINO=123 -DARDUINO=10604 -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DUSB_HID -DLAYOUT_US_ENGLISH -IC:\Program Files (x86)\arduino-1.6.4\hardware\teensy\avr\cores\teensy C:\Users\wolf\AppData\Local\Temp\build268063356590398533.tmp\key_error.cpp -o C:\Users\wolf\AppData\Local\Temp\build268063356590398533.tmp\key_error.cpp.o 

In file included from key_error.ino:1:0:
C:\Users\wolf\AppData\Local\Temp\build268063356590398533.tmp\Key_Null.h:7:1: error: expected class-name before '{' token
{
^
expected class-name before '{' token

The files are attached. Does it compile on your Arduino 1.6.4 IDE?

Thank you.

key_error.zip (5.15 KB)

Tried this with Arduino 1.6.3 and the code indeed compiles.

All I did was extract the files to Documents\Arduino\Libraries\KeyError and moved key_error.ino to Examples\key_error.

Perhaps you have a conflicting library in your Documents\Arduino\Library folder?