C++
C++ No Member Named Fix
When you write object.name, the compiler looks the name up in the object's class, and in that class's base classes. If the name is not there, compilation stops with 'has no member named'. The name can be missing for different reasons: it is spelled wrong, it belongs to a different type, it lives only in a derived class, it was added in a newer standard than the one you are compiling with, or you called it on the wrong variable. This guide separates those cases, reads what the diagnostic actually reports, and matches each one to its fix.
Written November 25, 2026 · Tested with GCC 13.3.0 · 6 min read
- The rule underneath: member lookup looks in the object's class
- How to read the diagnostic
- The first failure: a misspelled member name
- The second failure: a member that belongs to a different type
Written November 25, 2026
Tested with GCC 13.3.0
What this guide is
In object.name, name has to be a member of the object's class, or of one of that class's base classes. If it is not, compilation stops with "'class X' has no member named 'name'". The class named in the error is the class of the object on the left of the dot, and the member named in the error is the name on the right - the compiler is telling you exactly where it looked and what it did not find.
The causes in this guide each reproduce the failure with a small, complete program: a misspelled member name, a member that belongs to a different standard container, a derived-class member called through a base-class object, a member that only exists from C++23 called under C++20, and a member called on the wrong variable. In each family the failing program is small, the compiler output is shown, and the fix changes the name, the type, the object, or the standard level so the lookup succeeds.
Build every program in this guide with g++ -std=c++20 -Wall -Wextra, except where a family says otherwise. The failing programs are compiled with -c - compile only - because the error prevents any program from being produced; the fixed programs are compiled and run, so their output is shown.
This guide is about the name not being a member at all. It is a different diagnostic from the one for using a dot on a pointer, which is covered separately in the guide to "request for member in pointer type".
The rule underneath: member lookup looks in the object's class
The built-in member access operators define E1.E2 this way: E1 must be an expression of complete class type T, and E2 must name a member of T or of an unambiguous and accessible base class of T. That is the whole lookup. The compiler does not search other classes, does not search classes derived from T, and does not search for a similar name in a different type.
Three consequences of that rule explain the families below. First, spelling is exact: a name that differs from a real member by two transposed letters is not that member, even when a human reader can see what was meant. Second, lookup goes up the inheritance chain, never down: a base-class object does not gain the members a derived class adds, because those members are not members of the base. Third, for a standard library type, the member set depends on the standard level you compile with: a member added in C++23 is not a member of the class when the same header is compiled as C++20.
That gives a fast way to read any instance of this error: take the class and the member name from the diagnostic, then ask which of those three the code has wrong - the spelling, the direction or the type, or the standard level - or whether the object on the left is simply not the object the call was written for.
How to read the diagnostic
Every failure in the main families prints the same shape of output. The first family's, in full:
fail_typo.cpp: In function ‘int main()’:
fail_typo.cpp:11:20: error: ‘class Person’ has no member named ‘getNmae’; did you mean ‘getName’?
11 | std::cout << p.getNmae() << "\n";
| ^~~~~~~
| getName
Read it in order. The first line gives the file and the function. The error line names the line and column of the member name after the dot, and the caret sits under that name in your line. The two names in the message do the diagnostic work: the class is the type the compiler looked in, and the quoted member is the name it looked for. When GCC can see a close match, it adds "did you mean" and repeats the suggestion under the caret - take that suggestion seriously, but check it: a suggestion is a guess at the intended name, and in the other families there is no close match in that class at all, so no suggestion appears.
The first failure: a misspelled member name
A program that calls a member with two letters transposed:
#include <iostream>
#include <string>
class Person {
public:
std::string getName() const { return "Ada"; }
private:
std::string name_ = "Ada";
};
int main() {
Person p;
std::cout << p.getNmae() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_typo.o fail_typo.cpp
fail_typo.cpp: In function ‘int main()’:
fail_typo.cpp:11:20: error: ‘class Person’ has no member named ‘getNmae’; did you mean ‘getName’?
11 | std::cout << p.getNmae() << "\n";
| ^~~~~~~
| getName
The class does have the member the code wants - spelled getName. Member lookup is exact, so getNmae is a different name, and a different name is no member at all. This is the one family where the fix is almost always sitting in the diagnostic itself, in the "did you mean" part.
The fix uses the spelling the class declares:
#include <iostream>
#include <string>
class Person {
public:
std::string getName() const { return "Ada"; }
private:
std::string name_ = "Ada";
};
int main() {
Person p;
std::cout << p.getName() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_typo fix_typo.cpp
$ ./fix_typo
Ada
The output is Ada. Only the call site changed. If the diagnostic offers no suggestion, the name is not a near miss of anything in that class - work through the other families instead of guessing spellings.
The second failure: a member that belongs to a different type
A program that calls push_front on a std::vector:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{1, 2, 3};
v.push_front(0);
std::cout << v.size() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_wrongtype.o fail_wrongtype.cpp
fail_wrongtype.cpp: In function ‘int main()’:
fail_wrongtype.cpp:5:7: error: ‘class std::vector<int>’ has no member named ‘push_front’
5 | v.push_front(0);
| ^~~~~~~~~~
push_front is a real member function - just not of std::vector. The std::vector member list has push_back for adding at the end and insert for adding elsewhere, and no push_front. The member exists on types built for adding at the front: std::deque lists push_front among its modifiers, and so does std::list. The same shape appears with std::string, which also has no member named push_front. There is no "did you mean" here, because nothing in std::vector is close to that name.
The fix picks the type that has the member the code needs. Here the intent is to add at the front, so the container changes:
#include <deque>
#include <iostream>
int main() {
std::deque<int> v{1, 2, 3};
v.push_front(0);
std::cout << v.size() << " " << v.front() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_wrongtype fix_wrongtype.cpp
$ ./fix_wrongtype
4 0
The output is 4 0: four elements, with the new 0 at the front. If the container type cannot change, keep the vector and use what it does have - v.insert(v.begin(), 0) adds the same element at the front. Choose based on which side is fixed: the operation the code needs, or the type the rest of the code depends on.
The third failure: a derived-class member called through a base object
A program that calls a derived class's member on a base-class object:
#include <iostream>
class Base {
public:
void print() const { std::cout << "base\n"; }
};
class Derived : public Base {
public:
void extra() const { std::cout << "extra\n"; }
};
int main() {
Base b;
b.extra();
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_base.o fail_base.cpp
fail_base.cpp: In function ‘int main()’:
fail_base.cpp:12:7: error: ‘class Base’ has no member named ‘extra’
12 | b.extra();
| ^~~~~
extra exists, and it is closely related to Base - but lookup runs from the object's class upward through its bases, never downward into classes derived from it. Base has no member named extra, and it cannot gain one from Derived: code that only has a Base object has no Derived part in it to call the member on. The same failure appears when the object is reached through a Base pointer or a Base reference bound to a Derived object: the type on the left of the dot is still Base, so the lookup is still in Base.
The fix uses an object of the class that actually has the member:
#include <iostream>
class Base {
public:
void print() const { std::cout << "base\n"; }
};
class Derived : public Base {
public:
void extra() const { std::cout << "extra\n"; }
};
int main() {
Derived d;
d.extra();
d.print();
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_base fix_base.cpp
$ ./fix_base
extra
base
The output is extra then base. The Derived object has both members: its own extra, and print inherited from Base, which is the upward lookup working as designed. If the code genuinely has only a base object and needs derived behavior, that is a design question - a virtual function on Base that Derived overrides is the usual answer - not a spelling fix.
The fourth failure: a member that only exists in a newer standard
A program that calls contains on a std::string, compiled as C++20:
#include <iostream>
#include <string>
int main() {
std::string s = "hello world";
std::cout << s.contains("world") << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_version.o fail_version.cpp
fail_version.cpp: In function ‘int main()’:
fail_version.cpp:5:20: error: ‘std::string’ {aka ‘class std::__cxx11::basic_string<char>’} has no member named ‘contains’
5 | std::cout << s.contains("world") << "\n";
| ^~~~~~~~
The name is spelled right, and it is a real member of std::string - from C++23. std::basic_string::contains is marked "(since C++23)" for all three of its overloads. Compiled as C++20, the class the header presents has no such member, so the lookup fails exactly as if the name had never existed. Compiling this same file with -std=c++23 succeeds and the program prints 1. A missing #include is not the problem here: <string> is included, and the error is about the member, not the type.
The fix, when the build must stay on C++20, uses the older member that does the same job - cppreference states the contains overloads are equivalent to find(x) != npos:
#include <iostream>
#include <string>
int main() {
std::string s = "hello world";
std::cout << (s.find("world") != std::string::npos) << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_version fix_version.cpp
$ ./fix_version
1
The output is 1. If the whole project can move to C++23, changing the -std= flag is the other fix, and the original contains call stands unchanged. What does not work is adding an include or a using declaration: the member is absent from the class at this standard level, not hidden in another header.
The fifth failure: a member called on the wrong variable
A program with two objects of different classes, where the call lands on the wrong one:
#include <iostream>
#include <string>
class Dog {
public:
void bark() const { std::cout << "woof\n"; }
};
class Cat {
public:
void meow() const { std::cout << "meow\n"; }
};
int main() {
Dog d;
Cat c;
c.bark();
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_wrongvar.o fail_wrongvar.cpp
fail_wrongvar.cpp: In function ‘int main()’:
fail_wrongvar.cpp:14:7: error: ‘class Cat’ has no member named ‘bark’
14 | c.bark();
| ^~~~
fail_wrongvar.cpp:12:9: warning: unused variable ‘d’ [-Wunused-variable]
12 | Dog d;
| ^
bark is spelled right and it exists - on Dog. The diagnostic names Cat, because c is the object on the left of the dot, and that mismatch is the tell: when the class in the error is not the class you thought you were calling, the variable is wrong, not the member name. Here the warning underneath points the same way - d, the object the call was meant for, is never used. That warning is a clue in this program, not part of the error; a wrong-variable call where both objects are used elsewhere comes with no warning at all.
The fix calls each member on its own object:
#include <iostream>
#include <string>
class Dog {
public:
void bark() const { std::cout << "woof\n"; }
};
class Cat {
public:
void meow() const { std::cout << "meow\n"; }
};
int main() {
Dog d;
Cat c;
d.bark();
c.meow();
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_wrongvar fix_wrongvar.cpp
$ ./fix_wrongvar
woof
meow
The output is woof then meow. Before rewriting a call that fails this way, check the declarations of the variables in the function: same-shaped names for objects of different classes (c and d here; in real code, pairs like user and account) are where this family comes from.
The fixes in reach order
- The diagnostic says "did you mean": fix the spelling at the call site to the suggested name, after confirming that is the member you intended.
- The member is real but belongs to another type: change the object's type to the one that has the member, or rewrite the call with a member the current type does have.
- The class in the error is a base of the class that defines the member: use an object of the derived class, or move the shared behavior into the base as a virtual function.
- The member is documented as added in a newer standard than your
-std=flag: raise the flag for the whole project, or rewrite with the older equivalent the documentation names. - The class in the error is not the class you meant to call: check the variable on the left of the dot against its declaration - the member name is probably right and the object is wrong.
- The wording is different - "is private within this context", "request for member", "which is of pointer type", "which is of non-class type": that is a neighboring problem, not this one - see the traps.
Traps
- A member that exists but is private does not produce this error. The lookup finds the member, and access control rejects the call afterward, with different wording:
#include <iostream>
class Vault {
int code_ = 42;
public:
int getCode() const { return code_; }
};
int main() {
Vault v;
std::cout << v.code_ << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_private.o trap_private.cpp
trap_private.cpp: In function ‘int main()’:
trap_private.cpp:9:20: error: ‘int Vault::code_’ is private within this context
9 | std::cout << v.code_ << "\n";
| ^~~~~
trap_private.cpp:3:9: note: declared private here
3 | int code_ = 42;
| ^~~~~
trap_private.cpp:9:20: note: field ‘int Vault::code_’ can be accessed via ‘int Vault::getCode() const’
9 | std::cout << v.code_ << "\n";
| ^~~~~
| getCode()
->:#include <iostream>
#include <string>
int main() {
std::string s = "hi";
std::string* p = &s;
std::cout << p.size() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_pointer.o trap_pointer.cpp
trap_pointer.cpp: In function ‘int main()’:
trap_pointer.cpp:6:20: error: request for member ‘size’ in ‘p’, which is of pointer type ‘std::string*’ {aka ‘std::__cxx11::basic_string<char>*’} (maybe you meant to use ‘->’ ?)
6 | std::cout << p.size() << "\n";
| ^~~~
The member size is fine and std::string has it; the object expression is a pointer, so the access operator is wrong. Change . to -> (or dereference first) rather than touching the member name.
- A member call on a value that is not a class object at all also reports "request for member", with the non-class type named:
#include <iostream>
int main() {
int x = 5;
std::cout << x.size() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_int.o trap_int.cpp
trap_int.cpp: In function ‘int main()’:
trap_int.cpp:4:20: error: request for member ‘size’ in ‘x’, which is of non-class type ‘int’
4 | std::cout << x.size() << "\n";
| ^~~~
An int has no members to look up, so the compiler never reaches the "has no member named" stage. This wording means the variable on the left is a different kind of thing than the code assumes - check its declaration before looking for a member name to fix.
Checklist
- The class named in the diagnostic is the class you intended to call on - if not, fix the variable before the member name.
- The member name at the call site matches the declaration character for character, including any "did you mean" suggestion you accepted.
- The member belongs to that class (or one of its bases) - not to a sibling type, not only to a derived class.
- For a standard library member, the member's "since" version is at or below the
-std=level the build uses. - A private-member error is fixed through the class's public access, not by treating the member as missing.
- A pointer is accessed with
->, and no member call is made on a non-class value. - The fixed program run, and its output checked against what the program was meant to do.
Related guides on this site
- 'std::unique' Is Not a Member of 'std' Fix
- 'std::search_n' Is Not a Member of 'std' Fix
- 'std::replace' Is Not a Member of 'std' Fix
- 'std::remove' Is Not a Member of 'std' Fix
Sources
- cppreference, Member access operators - for E1.E2 requiring E1 to be of complete class type T and E2 to name a member of T or of an unambiguous and accessible base class of T.
- cppreference, std::vector - for the std::vector member function list, which has push_back and insert and no push_front.
- cppreference, std::deque - for std::deque allowing insertion at both ends and listing push_front among its member functions.
- cppreference, std::basic_string::contains - for all contains overloads being since C++23, and for each overload being equivalent to return find(x) != npos.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.