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


  bin / libUsersMgmnt.a(CUsersMgmnt.cpp.o):在函数中
  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):
  截断重定位以适合:R_X86_64_PC32针对未定义的符号
  `int
  nsMsgHandler :: CMsgHandler :: createLocationUserMap()'
  collect2:错误:ld返回1退出状态


我有这个基类:

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中实例化您的模板

09-04 16:54