gpt4 book ai didi

C++在派生类中调用基类的模板方法

转载 作者:行者123 更新时间:2023-11-28 02:56:15 26 4
gpt4 key购买 nike

我在 cygwin 中使用 gcc 4.8.2 进行编译,编译完成时没有出现错误。但是当链接时,我收到以下消息:

bin/libUsersMgmnt.a(CUsersMgmnt.cpp.o): In function nsUserMgmnt::CUsersMgmnt::CUsersMgmnt()':
/home/HCAST2/v1.05-dev/UsersMgmnt/CUsersMgmnt.cpp:23: undefined
reference to
int nsMsgHandler::CMsgHandler::createLocationUserMap()' /home/HCAST2/v1.05-dev/UsersMgmnt/CUsersMgmnt.cpp:23:(.text+0x19f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `int nsMsgHandler::CMsgHandler::createLocationUserMap()' collect2: error: ld returned 1 exit status

我有这个基类:

Header File CMsgHandler.h

namespace nsMsgHandler
{
class CMsgHandler
{
protected:
template<class enhFlags> createLocationUserMap();
};
}

代码文件CMsgHandler.cpp

... some code
using namespace nsMsgHandler;
... some code
template <class enhFlags>
int CMsgHandler::createLocationUserMap()
{
.....
}

这是派生类: 头文件CUsersMgmnt.h

namespace nsUserMgmnt
{
class CUsersMgmnt : public CMsgHandler
{
public:
CUsersMgmnt();
};
}

代码文件CUsersMgmnt.cpp

... some code
using namespace nsUserMgmnt;
... some code
CUsersMgmnt::CUsersMgmnt()
{
this->createLocationUserMap<nsUserMgmnt::types::Class1>();
}

我很确定代码中有错误。我试图解决这个问题几个小时。

最佳答案

你应该移动你的:

template <class enhFlags>
int CMsgHandler::createLocationUserMap()
{
.....
}

到CMsgHandler.h

否则编译器无法在 CUsersMgmnt.cpp 中实例化您的模板

关于C++在派生类中调用基类的模板方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21919004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com