首页 文章

从另一个类访问静态成员函数

提问于
浏览
3

我在C类中有一个静态stl映射,并有另一个静态成员函数来返回一个指向映射中对象的常量指针 . 此映射对于类中的所有对象都是通用的 .

唯一的问题是,我需要搜索这个 Map 并从另一个类中设置它,这个类在不同的.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")

来自其他 class . 网上的其他帖子似乎谈论使用extern,但我不确定 . 我不明白为什么这与从另一个类调用任何其他成员函数有什么不同?

谢谢,詹姆斯

编辑:是的,我已经宣布了

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

在Timestream.cpp文件的顶部 . 在 Headers 中,它被定义为

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

我已将RoomDescription的 Headers 包含在我试图从中调用这些方法的类的 Headers 中 .

我得到的错误就是这个

Import.obj:错误LNK2019:未解析的外部符号“public:static void __cdecl Timestream :: setRoomList(class std :: map,class std :: allocator>,class RoomDescription,struct std :: less,class std :: allocator> >,类std :: allocator,类std :: allocator> const,类RoomDescription >>>)“(?setRoomList @Timestream @@ SAXV?$ map @ V?$ basic_string @ DU?$ char_traits @ D @ std @@ V'$分配器@ d @ @@ 2 STD @@ VRoomDescription @@ U&$ @少V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@@ 2 @ V'$分配器@ U&$对@ $$ CBV?$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ 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 @ Ti mestream @@ 0V?$ Map @ V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@ VRoomDescription @@ U&$ @少V'$ basic_string的@ DU ?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@@ 2 @ V'$分配器@ U&$ @配对$$ CBV?$ basic_string的@ DU?$ char_traits @ d @性病@@ V'$分配器@ d @ @@ 2 STD @@ VRoomDescription @@@ STD @@@ 2 @@ STD @@ A)

2 回答

  • 2

    你需要添加:

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

    Timestream.cpp ,或者调用任何实现文件 .

    这将定义静态成员 . 在头文件中,您只需声明它 .

  • 3

    我猜测未解析的外部符号是 roomList ;这是错误消息中包含的重要信息 . 并且,据推测, roomList 是's been declared in the class definition. If those assumptions are right, then the cause of the error is that the code doesn'具有 definition roomList 的静态成员 .

    通常,静态成员必须在类定义中为 declared ,在源文件中为 defined

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

    这与从另一个类访问静态成员无关 . 如果您尝试从声明它的类中访问它,则会收到相同的错误 .

    编辑:从新发布的错误消息中,有两个缺少的名称: setRoomListroomList . 请注意 roomListTimestream 的成员,必须这样定义,也就是说,它的定义必须包含 Timestream:: . 如上所示,它只是一个全局数据对象,而不是 Timestream 的成员 . setRoomList 的问题可能相同 .

相关问题