C++ 中的类类型重定义

在本指南中,我们将了解 C++ 中的错误类类型重新定义以及如何避免此错误。当你在编程中使用类时,有些事情是你不能做的。

让我们了解这些方面并学习如何解决此错误。

C++ 中的类类型重定义

当你用相同的名字定义一个类两次时,C++ 编译器会抛出一个错误:class type redefinition。例如,看看下面的代码。

#include<iostream>using namespace std;
// #include student.h
//when you define a class twice with same name then you will get an error class type redefinition
class student
{
};
class student
{
};
// // the best way to solve the error is to define classes with different name
// class student_
// {
// };

因此,你不能定义两个具有相同名称的类。避免此错误的最佳方法是创建具有不同名称的类。

#include<iostream>using namespace std;
// #include student.h
//when you define a class twice with same name then you will get an error class type redefinition
class student
{
};
// // the best way to solve the error is to define classes with a different name
class student_
{
};

同理,我们不能定义同名的变量,但是我们可以定义同名的函数,这个概念叫做函数重载。