【问题标题】:Accessing a static member function from another class从另一个类访问静态成员函数
【发布时间】:2012-08-23 02:31:03
【问题描述】:

我在 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")

来自其他班级。网络上的其他帖子似乎在谈论使用 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::分配器 > >,类 std::allocator,类 std::allocator > const ,类 RoomDescription> > >)" (?setRoomList@Timestream@@SAXV?$map@V?$basic_string@DU?$char_traits@D@std @@V?$allocator@D@2@@std@@VoomDescription@@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@@VoomDescription@@@std@ @@2@@std@@@Z) 在函数“public: int __thiscall Import::getRoomData(void)”中引用(?getRoomData@Import@@QAEHXZ)

Timestream.obj:错误 LNK2001:未解析的外部符号“私有:静态类 std::map,类 std::allocator >,类 RoomDescription,struct std::less,类 std::allocator > >,类 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@@VoomDescription@@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)

【问题讨论】:

  • 可能你没有在 .cpp 文件中定义 roomList 变量。无论如何,显示确切的代码和错误消息。
  • 可能重复(可能):stackoverflow.com/questions/7092765/…
  • setRoomList 定义在哪里?
  • 有两个问题,std::map<:string roomdescription> roomList;应该是 std::map<:string roomdescription> Timestream::roomList;令人难以置信的是,我忘记了定义 setRoomList 方法,这是我比较昏暗的时刻之一。感谢您对此的帮助。总是发现自己在使用静态变量/方法时遇到问题。

标签: c++


【解决方案1】:

你需要添加:

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

Timestream.cpp,或任何你调用的实现文件。

这将定义静态成员。在头文件中,你只声明它。

【讨论】:

    【解决方案2】:

    我猜未解析的外部符号是roomList;这是错误消息中包含的重要信息。而且,大概roomList 是在类定义中声明的静态成员。如果这些假设是正确的,那么错误的原因是代码没有roomList定义

    一般来说,静态成员必须在类定义中声明并在源文件中定义

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

    这与从另一个类访问静态成员无关。如果你试图从声明它的类中访问它,你会得到同样的错误。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 2022-11-13
      • 2018-10-05
      • 2021-08-29
      相关资源
      最近更新 更多