HOWTO · C++

C++ でのクラス型の再定義

この記事では、C++ でクラス型の再定義を解決する方法について説明します。

このガイドでは、C++ でのエラークラスタイプの再定義と、このエラーを回避する方法について学習します。プログラミングのクラスで作業している間はできないことがいくつかあります。

それらの側面について学び、このエラーを解決する方法を学びましょう。

C++ でのクラス型の再定義

同じ名前でクラスを 2 回定義すると、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_
// {

// };

したがって、同じ名前で 2つのクラスを定義することはできません。このエラーを回避する最善の方法は、異なる名前のクラスを作成することです。

#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_ {};

同様に、同じ名前の変数を定義することはできませんが、同じ名前の関数を定義することはできます。その概念は、関数のオーバーロードと呼ばれます。