简体   繁体   English

具有模板成员函数的C ++继承类

[英]C++ Inherit class with template member function

I´m having problems while writing a Resource class: 编写Resource类时遇到问题:

class BaseResource {
protected:
    unsigned int size;

public:
    virtual ~BaseResource() {}
    template<class T> const T& GetValue() const;
    template<class T, class U> void GetValue(const U& rhs);

    unsigned int GetSize() {
        return this->size;
    }
    void SetSize(unsigned int size) {
        this->size = size;
    }
};

template<class T>
class Resource : public BaseResource {
    T value;

public:
    virtual ~Resource() {}      
    Resource(unsigned int size, const T& rhs) { this->size = size; this->value = rhs; }

    const T& GetValue() const {return value;}
    void SetValue(const T& rhs) {value=rhs;}  
};

I think that the classes above are defined correctly so I do not understand why the following code produce a linker error: 我认为上面的类定义正确,所以我不明白为什么以下代码会产生链接器错误:

Test.obj : error LNK2001: unresolved external symbol ""public: char * const & __thiscall BaseResource::GetValue(void)const " (??$GetValue@PAD@BaseResource@@QBEABQADXZ)". Test.obj:错误LNK2001:无法解析的外部符号“” public:char * const&__thiscall BaseResource :: GetValue(void)const“(?? $ GetValue @ PAD @ BaseResource @@ QBEABQADXZ)”。

char* c = new char[3];
c[0] = '1';
c[1] = '2';
c[2] = '3';
BaseResource* resource = new Resource<char*>(3, c);
char* loadedResource = resource->GetValue<char*>();

In my opinion this should create an instance of Resource which holds a char* and can return it. 在我看来,这应该创建一个Resource实例,该实例保存一个char *并可以返回它。

Can anybody tell me where I`ve done the mistake causing this? 谁能告诉我我在哪里造成了这个错误?

Following methods are not implemented : 未实现以下方法:

template<class T> const T& GetValue() const;
template<class T, class U> void GetValue(const U& rhs);

I hope you are not planning on making them virtual because that wil not work. 我希望您不打算将它们虚拟化,因为那样行不通。 Template methods cannot be made virtual. 模板方法不能设为虚拟。 As they are not implemented that definitely explains the linking problem. 由于未实现,因此肯定可以解释链接问题。

The implementation of these function should be in the same header as the class. 这些函数的实现应与该类位于同一标头中。 In this case you have instantiated the template function and the concrete instantiated function is not defied. 在这种情况下,您已实例化了模板函数,并且未定义具体的实例化函数。 Any time you use templates you need to include the definition of the function in the translation unit that uses the function. 每次使用模板时,都需要在使用该功能的翻译单元中包括该功能的定义。

Edit 编辑


This is the basic Idea. 这是基本思想。 You need to have the implantation defined so that when the class is instantiated the class is fully defined. 您需要定义植入,以便在实例化该类时完全定义该类。

public:
    virtual ~BaseResource() {}
    template<class T> const T& GetValue() const
    {
       return someT;
    }
    template<class T, class U> void GetValue(const U& rhs)
    {
       return someT;
    } 

    unsigned int GetSize() {
        return this->size;
    }
    void SetSize(unsigned int size) {
        this->size = size;
    }
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM