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

같은 방식으로 같은 이름의 변수를 정의할 수는 없지만 같은 이름의 함수는 정의할 수 있으며 그 개념을 함수 오버로딩이라고 합니다.