1

私はファイルを持っています:

variableinclude.h

#ifndef _variableinclude_h_
#define _variableinclude_h_

AClass* variable1;
int* variable2;

#endif

しかし、このファイルを別の 2 つのファイルに含めます。

- atest1.h

- atest2.h

問題は次のとおりです。変数の再定義。

それを回避するには???

4

1 に答える 1

9

EDIT2:

ODRへようこそ

編集1:

ヘッダー ファイルで変数を extern にします。

extern AClass* variable1;   // assuming AClass is declared at this point.
extern int* variable2;

名前空間スコープの main.cpp など、任意の cpp ファイルで一度だけ定義します。

AClass* variable1 = NULL;   // assuming AClass is declared at this point.
int* variable2 = NULL;
于 2010-10-01T05:02:11.183 に答える