아마도이 헤더 파일을 하나의 번역 단위에 두 번 이상 포함시킬 것입니다. 파일이 두 x 째로 포함될 때 struct udtCharVec
이이 L 정의 되었으 G로 "유형 재정의"오류가 발생합니다.
는
include guard를 추가합니다. 첫 번째 포함하면,
CharVec_H
정의됩니다, 그래서 파일의 나머지를 건너 뜁니다 :
#ifndef CharVec_H
#define CharVec_H
#include "stdafx.h"
#include <string>
using namespace std
struct udtCharVec
{
wstring GraphemeM3;
wstring GraphemeM2;
};
#endif
프로젝트는 세 개의 파일로 구성 말한다. 두 헤더 파일과 하나의 소스 파일 :
CharVec.h
#include "stdafx.h"
#include <string>
using namespace std
struct udtCharVec
{
wstring GraphemeM3;
wstring GraphemeM2;
};
CharMatrix.h
#include "CharVec.h"
struct udtCharMatrix
{
CharVec vec[4];
};
프리 프로세서를 실행 한 후 MAIN.CPP
#include "CharVec.h"
#include "CharMatrix.h"
int main() {
udtCharMatrix matrix = {};
CharVec vec = matrix.vec[2];
};
, 주. cpp는 다음과 같습니다 (표준 라이브러리 포함 안 함) :
//#include "CharVec.h":
#include "stdafx.h"
#include <string>
using namespace std
struct udtCharVec //!!First definition!!
{
wstring GraphemeM3;
wstring GraphemeM2;
};
//#include "CharMatrix.h":
//#include "CharVec.h":
#include "stdafx.h"
#include <string>
using namespace std
struct udtCharVec //!!Second definition!!
{
wstring GraphemeM3;
wstring GraphemeM2;
};
struct udtCharMatrix
{
CharVec vec[4];
};
int main() {
udtCharMatrix matrix = {};
CharVec vec = matrix.vec[2];
};
이 확장 파일 0 struct udtCharVec
의 두 가지 정의를 포함한다. CharVec.h
에 포함 보호를 추가하면 두 번째 정의가 전처리 기에서 제거됩니다. 그런
구조체를 정의한 현재 파일에 오류가 있습니까? 또는이 코드를 다른 파일의 포함 파일로 사용하고 있습니까? – Dineshkumar
문제 외에도 헤더 파일에서 "using directive"를 사용하지 마십시오. http://stackoverflow.com/questions/4872373/why-is-including-using-namespace-into-a-header-file- a-bad-idea-in-c –
헤더 파일의 사용 코드는 어디에 있습니까? 귀하의 질문을 수정하고 게시하십시오 ... – pinkpanther