HOWTO · C++
C++ 中的類型別重定義
本文討論如何解決 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_ {};
同理,我們不能定義同名的變數,但是我們可以定義同名的函式,這個概念叫做函式過載。