How to Resolve a Name Conflict in C++?
Last Updated :
11 Mar, 2024
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:
C++
// C++ Program to illustrate the name confict error
#include <iostream>
using namespace std;
int main()
{
int x = 10;
// This will cause a Name Conflict Error
double x = 20.5;
return 0;
}
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:
- Global and Local Variables with the Same Name
- Function Overloading with Same Parameter Types
- Same Names in Different Namespaces
- Same Names in Class and Derived Class
- 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:
- Using Namespaces
- 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
C++
// C++ Program to demonstrate how to use namespaces to
// resovle name conflicts in two classes
#include <iostream>
#include <string>
// Define namespaces
namespace my_namespace1 {
class Student {
public:
std::string name;
int age;
// Constructor
Student(std::string name, int age)
: name(name)
, age(age)
{
}
// Method to display student details
void display()
{
std::cout << "Name: " << name << "\nAge: " << age
<< std::endl;
}
};
} // namespace my_namespace1
namespace my_namespace2 {
class Student {
public:
std::string name;
int age;
int roll;
// Constructor
Student(std::string name, int age, int roll)
: name(name)
, age(age)
, roll(roll)
{
}
// Method to display student details
void display()
{
std::cout << "Name: " << name << "\nAge: " << age
<< "\nRoll: " << roll << std::endl;
}
};
} // namespace my_namespace2
int main()
{
// Creating object of Student class from my_namespace1
std::cout << "Student 1 Details: " << std::endl;
my_namespace1::Student s1("John", 25);
s1.display();
// Creating object of Student class from my_namespace2
std::cout << "\nStudent 2 Details: " << std::endl;
my_namespace2::Student s2("Ravi", 30, 25);
s2.display();
return 0;
}
OutputStudent 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
C++
// C++ Program to demonstrate how to resolve name conflict
// in local and global variable
#include <iostream>
int x = 10; // Global variable
void func()
{
int x = 20; // Local variable
// Accessing the local variable 'x'
std::cout << "Local x: " << x << std::endl;
// Accessing the global variable 'x' using scope
// resolution operator
std::cout << "Global x: " << ::x << std::endl;
}
int main()
{
func();
return 0;
}
OutputLocal x: 20
Global x: 10
Similar Reads
How to Create a Smart Pointer in C++? A smart pointer in C++ simulates a pointer while also providing automatic garbage collection as it deallocates or frees associated memory when it goes out of scope, which helps prevent memory leaks and dangling pointers. In this article, we will learn how to create a smart pointer in C++. Creating a
4 min read
How to Override a Base Class Method in C++? In C++, method overriding allows us to redefine a function from a parent class (base class) in its child class (derived class) to modify its behavior for objects of the child class. In this article, we will learn how to override a base class method in the derived class in C++. Override Inherited Met
2 min read
C++ Program to Find Initials of a Name Given a string name, we have to find the initials of the name Examples: Input: Kamlesh Joshi Output: K J We take the first letter of all words and print in capital letter. Input: Jude Law Output: J L Input: Abhishek Kumar Bisht Output: A K B 1) Print first character in capital. 2) Traverse rest of t
2 min read
How to Access Elements of a Pair in C++? In C++, a pair container is defined in <utility> header that can store two values that may be of different data types so as to store two heterogeneous objects as a single unit. In this article, we will learn how to access elements of a pair in C++. Example:Input: myPair={10,G} Output: First El
2 min read
How to fix auto keyword error in Dev-C++ The auto keyword in C++ specifies that the type of the variable that is being declared will be automatically deducted from its initializer. In case of functions, if their return type is auto then that will be evaluated by return type expression at runtime. CPP // C++ program to illustrate the auto /
2 min read
How to Create a Class with Private and Public Members in C++? In C++, the classes are blueprints for creating objects with specific properties and methods that provide a feature of access specifiers to the user through which they can control the access of the data members present in a class. In this article, we will learn how to create a class with private and
3 min read