简体   繁体   English

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

[英]C++ calling a template method of a base clase in a derived class

I'm compiling in cygwin with gcc 4.8.2, and the compilation finishes without an error. 我正在用gcc 4.8.2编译cygwin,编译完成没有错误。 But when linked, I receive the following message: 但链接后,我收到以下消息:

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 bin / libUsersMgmnt.a(CUsersMgmnt.cpp.o):在函数nsUserMgmnt::CUsersMgmnt::CUsersMgmnt()': /home/HCAST2/v1.05-dev/UsersMgmnt/CUsersMgmnt.cpp:23: undefined reference to / home / HCAST2 / v1.05-dev /UsersMgmnt nsUserMgmnt::CUsersMgmnt::CUsersMgmnt()': /home/HCAST2/v1.05-dev/UsersMgmnt/CUsersMgmnt.cpp:23: undefined reference to int nsMsgHandler的nsUserMgmnt::CUsersMgmnt::CUsersMgmnt()': /home/HCAST2/v1.05-dev/UsersMgmnt/CUsersMgmnt.cpp:23: undefined reference to :: CMsgHandler :: createLocationUserMap()'/ home / HCAST2 / v1.05-dev /UsersMgmnt /CUsersMgmnt.cpp:23 :(。text + 0x19f):重定位被截断以适合:R_X86_64_PC32对未定义的符号`int nsMsgHandler :: CMsgHandler :: createLocationUserMap()'collect2:error:ld返回1退出状态

I have this Base Class: 我有这个基类:

Header File CMsgHandler.h

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

Code File CMsgHandler.cpp 代码文件CMsgHandler.cpp

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

This is the derived class: Header File CUsersMgmnt.h 这是派生类:头文件CUsersMgmnt.h

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

Code file CUsersMgmnt.cpp 代码文件CUsersMgmnt.cpp

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

I'm pretty sure that there is an error in the code. 我很确定代码中有错误。 I tried to resolve this problem for hours. 我试图解决这个问题几个小时。

You should move your: 你应该移动你的:

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

to CMsgHandler.h 到CMsgHandler.h

otherwise compiler is not able to instantiate your template in CUsersMgmnt.cpp 否则编译器无法在CUsersMgmnt.cpp中实例化您的模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++多态模板类:调用基类方法而不是派生类 - C++ Polymorphism template class : calling base class method instead of derived class C++:在派生的 class 成员上调用基础 class 方法 - C++: Calling base class method on derived class member 模板基类的C ++调用模板方法 - C++ calling template method of template base class 通过此指针调用受保护的基类方法,该指针在派生类(C ++)中转换为基类 - Calling protected base class method via this pointer casted to base class in derived class (C++) 从派生的variadic模板类调用基本模板的虚方法 - Calling virtual method of base template from derived variadic template class C ++,许多类是从一个类派生的,所有派生类都使用从其他事物派生的类,但它们的基使用那句的基 - C++, a number of classes derive from a class, all the derived classes use a class derived from something else but their base uses that clase's base C ++-从方法基类调用派生类中的重写方法 - C++ - Calling overridden method in derived class from method base class C ++基础和派生类方法的麻烦 - C++ base and derived class method trouble C ++模板:从基类调用派生的模板类的成员函数 - C++ templates: Calling member function of derived template class from base class C ++:在基本模板类中调用派生的专用虚拟函数 - C++: Calling derived specialised virtual functions within a base template class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM