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

Haider Ali 2023年10月12日
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_ {};

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

著者: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

関連記事 - C++ Class