C++ Error – Does not name a type
Last Updated :
24 Apr, 2025
In C++, one of the most common errors that programmers encounter is the “does not name a type” error. The error usually occurs when the programmer writes a piece of code in which the compiler is not able to recognize the type or definition for it.
In this article, we are going to explore the “does not name a type” error in C++ in detail, including the causes of the error, and some coded examples which can trigger the error along with its solutions.
Causes of the Error
The root causes of this error are the syntactical mistakes that happen due to the use of incorrect syntax. This error can occur when the programmer uses an undefined class member, undeclared class pointer, and incorrectly defined variables. Here are some reasons in detail as to why this error can occur:
- C++ uses header files to define classes, structs, and different data types. You can encounter the error if you do not include header files while writing the program. The compiler will not recognize the associated types and generate the “does not name a type” error.
- Syntax errors in your code can easily generate this error as the compiler will misinterpret the intended type names. Let’s have a detailed look at some of the reasons which can trigger this error.
Some of the more common causes of the given error are as follows:
1. Using a datatype that has not been defined yet
A class or struct must be defined before it is used in C++. If the user tries to use the undefined data type “Day” to declare the data member, then the compiler will generate the stated error.
C++
#include <iostream>
using namespace std;
class MyClass {
public :
Day Morning;
};
|
Output
/tmp/PIU8LcSCJx.cpp:8:5: error: 'Day' does not name a type
8 | Day Morning;
| ^~~
In the above code, the compiler will generate the error because we have used the “Day” class which is not defined.
2. Circular dependencies between classes
When the definitions of 2 classes depend on each other and one of them is not defined then it can lead to circular dependency between them which can generate the “does not name a type error”. A coded example of the error would be:
C++
#include <iostream>
using namespace std;
class ClassA {
public :
ClassB obj;
};
class ClassB {
public :
ClassA obj;
};
int main() {
return 0;
}
|
Output
/tmp/PIU8LcSCJx.cpp:6:5: error: 'ClassB' does not name a type; did you mean 'ClassA'?
6 | ClassB obj;
| ^~~~~~
| ClassA
In the above code, ClassB depends on ClassA which is not defined completely, and thus, the compiler will produce an error of “does not name a type” for you.
3. Wrong initialization of variables
A variable declaration should be done inside a function or else you are sure to get an error. For example, we create a struct and then declare two variables inside them and do not initialize them. Later in the code, we initialize the same variables outside the function body. The compiler will generate a “variable does not name a type error”. Let’s look at the code to understand what we are trying to say.
C++
#include <iostream>
using namespace std;
struct games{
int Football;
int Cricket;
int VolleyBall;
};
games activeGames;
activeGames.Football=5;
activeGames.Cricket=11;
activeGames.VolleyBall=6;
int main() {
return 0;
}
|
Output
/tmp/PIU8LcSCJx.cpp:11:1: error: 'activeGames' does not name a type
11 | activeGames.Football=5;
| ^~~~~~~~~~~
/tmp/PIU8LcSCJx.cpp:12:1: error: 'activeGames' does not name a type
12 | activeGames.Cricket=11;
| ^~~~~~~~~~~
/tmp/PIU8LcSCJx.cpp:13:1: error: 'activeGames' does not name a type
13 | activeGames.VolleyBall=6;
| ^~~~~~~~~~~
4. Not following the Syntax
Overall, all the causes are cases when there is a mistake in the syntax of the program. Writing incorrect syntaxes like misplacing semicolons, curly brackets, and function calls before the main() function. The user can also face this error if he does not have a basic understanding of the difference between various operators and uses incorrect datatypes.
The C++ error can be solved if the user is careful with the class definitions, define the variables properly, and adhere to the syntax of the language. Let’s look at the detailed solutions for this particular type of error.
Solutions of the “does not name a type” Error in C
We should take care of the following points to avoid the “does not name a type error” in C.
1. Defining the Datatype before using it
As we have seen, if you do not define a class or a struct and then later try to use it, the compiler will throw the “does not name a type error”. It is better to define the datatype you are using in the program.
2. Removing Circular Dependency between Classes
In the circular dependency case, we should use the forward declaration of the classes. This will help in avoiding the error by telling the compiler that the class with the given name has its definition present somewhere in the program.
3. Defining Variables while declaring Them
You should define the variables while declaring them as it is considered a good coding practice and prevents these types of errors. Let’s look at the correct code to solve this issue.
4. Following the Syntax Correctly
It is the ultimate solution to the problem which removes the root of this error. The user should double-check the syntax and ensure the following points while checking their code:
- All the statements end with a semicolon.
- Ensure that all the code stays within the curly braces.
- Removing function calls before the main() function.
- Specifying correct datatypes for the variables.
Conclusion
The “does not name a type” error in C++ usually pops up when the compiler is unable to understand your program. The problem could be a variable or a function and can be caused by a variety of factors, such as missing header files, typos in writing variables, or circular dependencies between classes. It is very important to carefully review the code and check for any missing include statements. The error can be solved by understanding the root causes of the “does not name a type” error.
Similar Reads
Derived Data Types in C++
The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. They are generally the data types that are created from the primitive data types and provide some additional functionality. In C++, there are four different derived data types: Table of Co
4 min read
Syntax Error in C++
Syntax plays a vital role and even a slight mistake can cause a lot of unexpected errors. One of these errors is the "expected unqualified id error" or "expected unqualified id before ' ' token" which can arise due to some common oversights while writing your code. In this article, we are going to d
6 min read
C++ Pointer To Pointer (Double Pointer)
In C++ a Pointer is a variable that is used to store the memory address of other variables. It is a variable that points to a data type (like int or string) of the same type and is created with the * operator. Syntax of a Pointer in C++: data_type_of_pointer *name_of_variable = & normal_variable
4 min read
C++ set for user define data type
The C++ STL set is a data structure used to store the distinct value in ascending or descending order. By default, we can use it to store system defined data type only(eg. int, float, double, pair etc.). And if we want to store user-defined datatype in a set (eg. structure) then the compiler will sh
2 min read
User Defined Data Types in C++
User defined data types are those data types that are defined by the user himself. In C++, these data types allow programmers to extend the basic data types provided and create new types that are more suited to their specific needs. C++ supports 5 user-defined data types: Table of Content ClassStruc
4 min read
C++ Data Types
Data types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory. C++ supports a wide variety of data ty
8 min read
User-Defined Data Types In C
Data Types are the types of data that can be stored in memory using a programming language. Basically, data types are used to indicate the type of data that a variable can store. These data types require different amounts of memory and there are particular operations that can be performed on them. T
4 min read
Write a C program that won't compile in C++
Although C++ is designed to have backward compatibility with C, there can be many C programs that would produce compiler errors when compiled with a C++ compiler. Following is the list of the C programs that wonât compile in C++: Calling a function before the declarationUsing normal pointer with con
4 min read
Type Inference in C++ (auto and decltype)
Type Inference refers to automatic deduction of the data type of an expression in a programming language. Before C++ 11, each data type needed to be explicitly declared at compile-time, limiting the values of an expression at runtime but after the new version of C++, keywords such as auto and declty
6 min read
Top 10 Reasons to Learn C++
C++ is a general-purpose, object-oriented programming language that was designed by Bjarne Stroustrup in 1979 to be an extension of the C language. It has the features of imperative, object-oriented as well as generic programming models. C++ also has some additional facilities to those in C such as
9 min read