2

LNK2001 に問題があります: 未解決の外部シンボル エラーです。名前空間にすべてのクラスがあり、複数のファイルを使用するグローバル変数を参照している場合にのみ表示されます。これが私のコードがどのように見えるかのサンプルコードです:

エンジン.h

#pragma once
namespace Engine{
    #include "Core.h"
    #include "Display.h"
    #include "Box.h"
    // ... some code...
}
using namespace Engine;

Core.cpp

#include "Engine.h"
// ...some code...

コア.h

extern Matrix matrix;

// ... some code...

ディスプレイ.cpp

#include "Engine.h"
Matrix matrix;

// ... some code...

Display.h

// ... some code...

Box.cpp

void Box::draw(PxShape *shape){
    // matrix=.. some code...
}

Box.h

// ... some code...

エラーメッセージ

1>Box.obj: エラー LNK2001: 未解決の外部シンボル "struct Engine::Matrix Engine::matrix" (?matrix@Engine@@3UMatrix@1@A)

名前空間にコメントすると、すべてが正常に機能します。名前空間を使用したいのは初めてで、これをどうするかわかりません。

4

1 に答える 1

4

あなたの#includeディレクティブ (したがってインターフェース定義) は の中namespace Engineにありますが、実装はそうではないようです。それはあなたにリンクエラーを与えています。

これらの各 .cpp ファイルのコード本体もラップする必要がありますnamespace Engine

すなわち:

 #include "engine.h"
 namespace Engine
 {
     // implementation goes here
 }
于 2013-07-06T17:24:01.427 に答える