4

C++ クラスに静的 stl マップがあり、マップ内のオブジェクトへの定数ポインターを返す別の静的メンバー関数があります。このマップは、クラス内のすべてのオブジェクトに共通です。

唯一の問題は、このマップを検索して、別の .cpp/.h ファイルにある別のクラスから設定する必要があることです。vs2010 でそれらをコンパイルしようとすると、未解決の外部シンボルが取得されます。メソッドは Timestream クラスで次のように定義されています。

static void setRoomList(std::map<std::string, RoomDescription> rl);
static RoomDescription * getRoom(std::string ref); 

どちらの関数も公開されているため、アクセスの問題はありません。これらの関数は、Timestream.cpp ファイルで通常どおり定義されています。つまり、

RoomDescription * Timestream::getRoom(std::string ref)
{
    std::map<std::string, RoomDescription>::iterator cIter= roomList.find(ref);

    if(cIter!=roomList.end())
        return &(cIter->second);

    return NULL;
}

私はこれを次のように呼ぼうとしています

RoomDescription *r =Timestream::getRoom("Bathroom")

他のクラスから。Web 上の他の投稿では、extern の使用について話しているようですが、それについてはよくわかりません。これが、別のクラスから他のメンバー関数を呼び出すのとなぜ違うのかわかりませんか?

ありがとう、ジェームズ

編集:はい、私は宣言しました

std::map<std::string, RoomDescription> roomList;

Timestream.cpp ファイルの上部にあります。ヘッダーでは、次のように定義されています

static  std::map<std::string, RoomDescription> roomList;

これらのメソッドを呼び出そうとしているクラスのヘッダーに RoomDescription のヘッダーを含めました。

私が得るエラーはこれです

Import.obj : エラー LNK2019: 未解決の外部シンボル "public: static void __cdecl Timestream::setRoomList(class std::map,class std::allocator >,class RoomDescription,struct std::less,class std::allocator > > ,class std::allocator,class std::allocator > const ,class RoomDescription> > >)" (?setRoomList@Timestream@@SAXV?$map@V?$basic_string@DU?$char_traits@D@std@@V ?$allocator@D@2@@std@@VRoomDescription@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2 @V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@VRoomDescription@@@std@@@2 @@std@@@Z) 関数 "public: int __thiscall Import::getRoomData(void)" で参照 (?getRoomData@Import@@QAEHXZ)

Timestream.obj : エラー LNK2001: 未解決の外部シンボル "private: static class std::map,class std::allocator >,class RoomDescription,struct std::less,class std::allocator > >,class std::allocator, class std::allocator > const ,class RoomDescription> > > Timestream::roomList" (?roomList@Timestream@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@ D@2@@std@@VRoomDescription@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$ allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@VRoomDescription@@@std@@@2@@std@ @A)

4

2 に答える 2

4

以下を追加する必要があります。

std::map<std::string, RoomDescription> Timestream::roomList;

to Timestream.cpp、または実装ファイルが呼び出されるものは何でも。

これにより、静的メンバーが定義されます。ヘッダー ファイルでは、それを宣言するだけです。

于 2012-08-28T14:42:57.413 に答える
3

未解決の外部シンボルはroomList;だと思います。これは、エラーメッセージに含まれている重要な情報です。そして、おそらく、roomListクラス定義で宣言されている静的メンバーです。これらの仮定が正しい場合、エラーの原因は、コードにの定義がないことですroomList

一般に、静的メンバーはクラス定義で宣言し、ソースファイルで定義する必要があります。

// foo.h:
class C {
    static int data;
};

// foo.cpp:
int C::data = 42;

これは、別のクラスから静的メンバーにアクセスすることとは何の関係もありません。宣言しているクラスからアクセスしようとすると、同じエラーが発生します。

編集:新しく投稿されたエラーメッセージから、2つの欠落した名前があります:setRoomListroomListroomListはのメンバーでありTimestream、そのように定義する必要があることに注意してください。つまり、その定義には。を含める必要がありますTimestream::。上に示したように、これは単なるグローバルデータオブジェクトであり、のメンバーではありませんTimestream。の問題setRoomListは同じかもしれません。

于 2012-08-28T14:44:24.193 に答える