Fixing "'class ...' has no member named" in Arduino
Beginner
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
'class LiquidCrystal_I2C' has no member named 'init' means the object before the dot has no function or variable called the name after it. Either the name is misspelled or has a capital letter wrong, or the library installed is a different one, or a different version, from the one the sketch was written for, and it calls that function something else. Check the spelling first, then open an example that came with the installed library and use the names it uses.
Mokxi compiles with clang, which words it no member named 'init' in 'LiquidCrystal_I2C'. The circuit above is a 16x2 I2C LCD on an Uno: its sketch scans the bus, prints "I2C device found at 0x27", and writes "Hello, inventor!" on the display. Open it, change Serial.println to Serial.Println and press Run: the build stops with no member named 'Println' in 'HardwareSerial', and the build tab says "It is spelled println, not Println." with a button that puts it right.
A spelling or a capital letter
Member names are case sensitive like everything else in C++: println, begin, setCursor, createChar. lcd.CreateChar(0, heart); stopped the build with no member named 'CreateChar' in 'LiquidCrystal_I2C', and arm.atach(9); on a Servo with no member named 'atach' in 'Servo'. GCC often adds a suggestion, such as did you mean 'attach'?.
The class named in the message tells you what the compiler thinks the object is. Serial is a HardwareSerial, so Serial.printn("hello") gave no member named 'printn' in 'HardwareSerial'. If the class is not the one you expected, the object was declared as something else.
Two libraries with the same name: init() or begin()
This is the famous one. Two different libraries are both called LiquidCrystal_I2C, with the same header name. The one in the Arduino Library Manager, "LiquidCrystal I2C" by Frank de Brabander, starts the display with lcd.init(), and its begin() wants the columns and rows. Another, often downloaded as a .zip from GitHub, has lcd.begin() with nothing in the brackets and no init() at all. A sketch written for one gives 'class LiquidCrystal_I2C' has no member named 'init', or a complaint about begin(), with the other.
Either change the call to the one your library has, or install the library the sketch was written for. Mokxi’s LiquidCrystal_I2C takes both: lcd.init(), lcd.begin() and lcd.begin(16, 2) all built, so a sketch from either tutorial works here. The same thing happens with other libraries that were rewritten between versions; the examples that came with the installed version always use the names it has.
A function from another library, or another kind of text
Names learned elsewhere often almost fit. Adafruit’s DHT library has readTemperature() and readHumidity(), so dht.readTemp() gave no member named 'readTemp' in 'DHT'. Arduino’s String has length(), not the size() of C++’s std::string, so name.size() gave no member named 'size' in 'String'. The library’s header lists every member it has, and its examples show the common ones.
Your own struct works the same way. With struct Reading { int value; unsigned long when; };, the line last.time = millis(); stopped with no member named 'time' in 'Reading': the member is called when.
A dot on something that has no members
A pointer to an object uses ->, not a dot. With Servo *active = &arm;, the line active.attach(9); gave member reference type 'Servo *' is a pointer; did you mean to use '->'?; GCC says request for member 'attach' in 'active', which is of pointer type 'Servo*' (maybe you meant to use '->' ?). Write active->attach(9);.
A plain number has no members at all. level.toString(), with level an int, gave member reference base type 'int' is not a structure or union from clang and request for member 'toString' in 'level', which is of non-class type 'int' from GCC. To turn a number into text, write String(level).
Servo arm(); looks like making a servo, but to C++ it declares a function called arm that returns a Servo. Nothing complains on that line. The error comes where the object is used: arm.attach(9); stopped with base of member reference is a function; perhaps you meant to call it with no arguments?, and GCC says request for member 'attach' in 'arm', which is of non-class type 'Servo()'. Drop the brackets: Servo arm; built. An object whose constructor takes values, such as LiquidCrystal_I2C lcd(0x27, 16, 2);, keeps them.
An array has no members either. With Reading readings[4];, the line readings.value = analogRead(A0); gave member reference base type 'Reading[4]' is not a structure or union, and GCC says request for member 'value' in 'readings', which is of non-class type 'Reading [4]'. Pick one element first: readings[0].value, or readings[i].value inside a for loop that goes through all four.
#include <Servo.h>
Servo arm;
Servo *active = &arm;
int level = 3;
void setup() {
Serial.begin(9600);
active->attach(9);
String text = "level " + String(level);
Serial.println(text);
}
void loop() {
}Questions
What does "has no member named" mean in Arduino?
The object before the dot has nothing called the name after it. The name is misspelled, or the library you have is a different one, or a different version, from the one the sketch was written for.
How do I fix "'class LiquidCrystal_I2C' has no member named 'init'"?
Your LiquidCrystal_I2C library starts the display with begin() instead of init(). Change lcd.init() to lcd.begin(), or install the "LiquidCrystal I2C" library from the Library Manager, which has init(). Mokxi’s takes either.
Is "no member named" the same error?
Yes. It is clang’s wording; GCC, which the Arduino IDE uses, says "'class ...' has no member named". Mokxi compiles with clang, so its build tab shows clang’s words.
How do I see which functions a library has?
Open one of the examples that came with it, or its header file, the .h named in the #include. Every function you can call on the object is declared there.
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.