Fixing "does not name a type" in Arduino
Beginner
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
'Servo' does not name a type means the compiler found a word at the start of a declaration, where a type such as int or Servo belongs, and it has never heard of that type. Most of the time the #include line for the library is missing. The other causes are a line of code sitting outside every function, a capital letter wrong, and a struct or class used above the line that declares it.
Mokxi compiles with clang, which words the same error unknown type name 'Servo'. The circuit above is a cat laser toy with two servos, and its sketch starts with #include <Servo.h>. Open it in the editor, delete that line and press Run: the build stops with two errors, one for pan and one for tilt, and the build tab says "Servo comes from a library the sketch does not include. Add #include <Servo.h> at the top of the sketch." with a button that puts the line back.
A library type without its #include
Servo, LiquidCrystal_I2C, DHT and Adafruit_NeoPixel are classes that a library declares in its header. Until the sketch includes that header, the compiler does not know them, and the line that makes the object is the first to fail: Servo pan;, LiquidCrystal_I2C lcd(0x27, 16, 2);. GCC then reports every later use of pan or lcd as not declared too, while clang stops at the declarations. Either way, fix the first error and the others go with it.
The fix is the #include at the top of the sketch, spelled exactly as the library spells it: #include <Servo.h>, #include <LiquidCrystal_I2C.h>. If the line is there and the error stays, the header may be the wrong one. With #include <LiquidCrystal.h> and a LiquidCrystal_I2C lcd(...) line, clang says unknown type name 'LiquidCrystal_I2C'; did you mean 'LiquidCrystal'?: the six-wire library is there, the I2C one is not. If the header cannot be found at all, the error is "No such file or directory" instead, and the library is not installed.
A line of code outside a function
Between functions you can declare things: variables, constants, objects, functions. You cannot do things there. A line such as Serial.begin(9600); above setup() looks to the compiler like the start of a declaration whose type is Serial, so GCC says 'Serial' does not name a type. Clang says unknown type name 'Serial' and then cannot use dot operator on a type. The same happens to lcd.init(); or lcd.backlight(); left above setup() when code is copied from a tutorial: 'lcd' does not name a type.
Move the line into setup(), if it should run once, or into loop(), if it should run every time. Making the object stays outside, where every function can see it; using it goes inside.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // making the object: outside is fine
void setup() {
Serial.begin(9600); // doing something: inside a function
lcd.init();
lcd.backlight();
lcd.print("hello");
}
void loop() {
}A capital letter wrong
C++ is case sensitive, and so are type names. Arduino’s text type is String, with a capital S, so string name = "Mokxi"; fails: clang says unknown type name 'string'; did you mean 'String'?, and Mokxi’s build tab adds "Arduino spells it String, not string." with a button that changes it. Int count = 0; fails the same way, with clang suggesting int. GCC’s newer versions make the same suggestions; older ones only say does not name a type.
The library types have their capitals too: Servo, LiquidCrystal_I2C, Adafruit_SSD1306. Copy them from the library’s own example rather than typing them from memory.
A struct or class used above the line that declares it
C++ reads a file from the top down, and a type has to be declared before the first line that uses it. A sketch that keeps its struct Reading { int value; }; at the bottom, below a loop() that makes a Reading and a function that returns one, stopped the build with four errors: unknown type name 'Reading' on lines 6, 11 and 12, and use of undeclared identifier 'takeReading' on line 6.
Move the struct to the top of the sketch, above setup(), and the same code builds. This one is worse in the Arduino IDE. The IDE writes a declaration for each of your functions near the top of the file before compiling it, and a function that returns Reading gets a declaration above the struct, so GCC reports 'Reading' does not name a type on a line you never wrote. Keeping every struct, class and enum at the top avoids it in both.
struct Reading {
int value;
};
void setup() {
Serial.begin(9600);
}
void loop() {
Reading r = takeReading();
Serial.println(r.value);
delay(1000);
}
Reading takeReading() {
Reading r;
r.value = analogRead(A0);
return r;
}uint8_t or byte in a .h or .cpp tab
The main .ino tab gets #include <Arduino.h> added for you, and that is where byte, uint8_t, String, pinMode() and the rest come from. A .h or .cpp tab gets nothing added. A header tab pins.h holding uint8_t ledPin();, with a pins.cpp beside it, stopped the build with unknown type name 'uint8_t' twice, once in each file, though the same line in the .ino tab would have built.
Start every .h and .cpp tab with #include <Arduino.h>. With that one line at the top of pins.h, the project built.
#include <Arduino.h>
uint8_t ledPin();Questions
What does "does not name a type" mean in Arduino?
The compiler met a word where a type belongs, at the start of a declaration, and does not know it as a type. Usually the library’s #include is missing; otherwise a line of code is outside a function, a capital letter is wrong, or a struct is declared below where it is used.
Why does it say "Serial does not name a type"?
A line such as Serial.begin(9600); is outside every function, so the compiler reads it as a declaration. Move it into setup(). Code that does something has to be inside a function.
Is "unknown type name" the same error?
Yes. It is clang’s wording for the same mistake; GCC, which the Arduino IDE uses, says "does not name a type". Mokxi compiles with clang, so its build tab shows clang’s words.
I have the #include and it still says LiquidCrystal_I2C does not name a type. Why?
Check that the header is the I2C one: #include <LiquidCrystal.h> brings the six-wire library, whose class is LiquidCrystal. If the header is right, the error is probably "No such file or directory" a few lines up, which means the library is not installed.
Keep going
Where the facts on this page come from
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.