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_
{
};
同理,我们不能定义同名的变量,但是我们可以定义同名的函数,这个概念叫做函数重载。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。