c++ - 实现内存管理模板类时未解析的外部符号

标签 c++ visual-studio memory-management pool

<分区>

我正在尝试创建一个池来管理会快速死亡和重生的小游戏对象的分配等。

为此我创建了一个池:

template <class T>
class Pool {

public:

    T* obtain()
    {
        T* obj = 0;

        if (_avaibles.size() > 0)
        {
            std::vector<T*>::iterator it = _avaibles.begin();
            obj = *it;
            _avaibles.erase(it);
        }
        else
            obj = new T();

        return obj;
    }

    void free(T* obj)
    {
        _avaibles.push_back(obj);
    }

    void clear()
    {
        std::vector<T*>::iterator it = _avaibles.begin();

        while (it != _avaibles.end())
        {
            T act = *it;
            delete act;
            ++it;
        }
    }

private:

    std::vector<T*> _avaibles;

};

问题是我得到了未解析的外部符号。池被放置为类的静态成员:

 static Pool<Ship> _shipPool;

这里是错误:

Error   16  error LNK2001: unresolved external symbol "private: 
static class Pool<class Ship> Asdf::_shipPool"  
(?_shipPool@Asdf@@0V?$Pool@VShip@@@@A)  C:\-\Asdf.obj

最佳答案

你不能像那样拆分一个模板。将实现放入 .hpp 文件中,一切都会 Shiny 。

引用Why can templates only be implemented in the header file?了解更多信息。

关于c++ - 实现内存管理模板类时未解析的外部符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21814118/

相关文章:

javascript - 编译嵌入式 SpiderMonkey 程序时出错

c++ - Boost 的数据驱动测试的连接运算符 `+` 损坏了第一列

visual-studio - 无法安装通用 Windows 应用程序更新工具

c++ - 在 Emacs 中使用 Visual Studio 6 C++ 编译器

ios - 需要澄清 iPhone 内存管理

c++ - 返回类型模板(enable_if)禁止什么?

c++ - glreadpixels 模板缓冲区总是抛出 GL_INVALID_OPERATION

c# - 使用 C# 检查用户是否已存在于 MySql 数据库中

c - 管理链表内存

c++ - 删除顺序、NULL 指针或内存泄漏