VOOZH about

URL: https://www.geeksforgeeks.org/cpp/resolve-name-conflict-in-cpp/

⇱ How to Resolve a Name Conflict in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Resolve a Name Conflict in C++?

Last Updated : 23 Jul, 2025

In C++, naming conflict occurs when two identifiers in the same scope have the same name and the compiler cannot infer which identifier to refer to when it is mentioned in the program.

In this article, we will discuss what are name conflicts, what are its causes, and how to resolve the name conflict in C++.

Name Conflict Error in C++

As discussed in the introduction, the name conflict occurs when two or more identifiers have the same name in the same scope.

For example, look at the following code snippet:


Output

main.cpp: In function ‘int main()’:
main.cpp:10:12: error: conflicting declaration ‘double x’
10 | double x = 20.5;
| ^
main.cpp:7:9: note: previous declaration as ‘int x’
7 | int x = 10;
| ^

Cases of Name Conflict Errors in C++

Above is not the only case of this error, it can occurs in many other cases. Below are some common cases of this error:

  1. Global and Local Variables with the Same Name
  2. Function Overloading with Same Parameter Types
  3. Same Names in Different Namespaces
  4. Same Names in Class and Derived Class
  5. Same Name of Function Object(Functor) and Function

How to Resolve Name Conflict Error in C++

We can easily revolve the name conflict error by renaming the identifier which is causing it. But if you really need to use the same name, here are some other methods to do that:

  1. Using Namespaces
  2. Using Scope Resolution Operator

1. Resolve Name Conflict Error Using Namespace

Namespace feature was introduced in C++ exactly for resolving the name conflicts. This feature provide a separate space of each idenfier with same name and we can access them using its namespace.

Example


Output
Student 1 Details: 
Name: John
Age: 25

Student 2 Details: 
Name: Ravi
Age: 30
Roll: 25

2. Resolve Name Conflict Error Using Scope Resolution Operator

This method is used to resolve the name conflict in the case where the local variable and global variable have same name. We just access the global variable using scope resolution operator.

Example


Output
Local x: 20
Global x: 10



Comment