提问者:小点点

为MemoryPool获取对typeinfo的未定义引用,我的所有方法都已定义


我知道这条信息的一般原因。不过,我不认为这会发生,除非我错过了什么。我认为问题是突然出现的,因为我是从一个带有模板类的抽象基中派生出来的,而它并没有因此解析所有的方法。麻烦的是,我不知道怎么修。我通常尽量不混合模板和继承,但在这种情况下,我需要固定大小的数组缓冲区,而我知道的实现这一点的唯一其他方法是使用预处理器而不是编译器w/templates。(原谅代码中的任何bug,我甚至还没有机会测试概念验证,因为它不会编译)

我得到了这个错误(略加剪裁):

In function `pool::MemoryPool::MemoryPool()':
MemoryPool.hpp:11: undefined reference to `vtable for pool::MemoryPool'
undefined reference to `typeinfo for pool::MemoryPool'

我已经在MemoryPool和StaticMemoryPool中尝试了所有构造函数和析构函数(虚拟和非)的组合,但都没有效果。

有人帮忙吗?

提前谢谢你。

代码如下:

struct MemoryPool {
    virtual void* alloc(const size_t size)=0;
    virtual void freeAll()=0;
    virtual void* next();
    virtual size_t capacity()=0;
    virtual size_t used()=0;
};
template<size_t C> class StaticMemoryPool : public MemoryPool {
    uint8_t _heap[C];
    uint8_t *_next;
public:
    void* alloc(const size_t size) override {
        if(used()+size>=C)
            return nullptr;
        void* result = _next;
        _next+=size;
        return result;
    }
    void freeAll() override {
        _next = _heap;
    }
    void *next() override {
        return _next;
    }
    size_t capacity() override { return C; }
    size_t used() override {return _next-_heap;}
    StaticMemoryPool() : _next(_heap) {}
    ~StaticMemoryPool() {}
};

共1个答案

匿名用户

MemoryPool::Next既没有实现,也没有抽象。

--伊戈尔·坦代特尼克

谢谢!你不知道我盯着那个代码寻找问题有多久了!