V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
linux40
V2EX  ›  C

当编译 StrVec 中的 reserve、resize、reallocate 函数时 gcc 报了如下错误?

  •  
  •   linux40 · 2015-05-18 22:08:17 +08:00 · 692 次点击
    这是一个创建于 3640 天前的主题,其中的信息可能已经有所发展或是发生改变。
    more undefined references to `StrVec::alloc' follow,而且这三个函数当中只出现一个函数的定义的话,则无错误,下面贴代码:
    #ifndef STRVEC_H
    #define STRVEC_H
    #include <memory>
    #include <string>
    #include <utility>
    #include <initializer_list>
    class StrVec
    {
    public:
    StrVec():
    elements(nullptr), first_free(nullptr), cap(nullptr) {}
    StrVec(std::initializer_list<std::string> sl)
    {
    auto ptr_pair = alloc_n_copy(sl.begin(), sl.end());
    elements = ptr_pair.first;
    first_free = cap = ptr_pair.second;
    }
    StrVec(const StrVec &sv)
    {
    auto ptr_pair = alloc_n_copy(sv.begin(), sv.end());
    elements = ptr_pair.first;
    first_free = cap = ptr_pair.second;
    }
    StrVec &operator=(const StrVec &sv)
    {
    auto ptr_pair = alloc_n_copy(sv.begin(), sv.end());
    free();
    elements = ptr_pair.first;
    first_free = cap = ptr_pair.second;
    return *this;
    }
    ~StrVec()
    {
    free();
    }
    void push_back(const std::string &s)
    {
    chk_n_alloc();
    alloc.construct(first_free++, s);
    }
    std::string *begin() const
    {
    return elements;
    }
    std::string *end() const
    {
    return first_free;
    }
    std::size_t size() const
    {
    return first_free - elements;
    }
    std::size_t capacity() const
    {
    return cap - elements;
    }
    void reserve(const std::size_t &sz);
    void resize(const std::size_t &sz, const std::string &s);
    private:
    static std::allocator<std::string> alloc;
    void chk_n_alloc()
    {
    if (first_free == cap)
    reallocate();
    }
    std::pair<std::string *, std::string *> alloc_n_copy
    (const std::string *b, const std::string *e)
    {
    auto new_elem = alloc.allocate(e - b);
    return {new_elem, std::uninitialized_copy(b, e, new_elem)};
    }
    void free();
    void reallocate();
    std::string *elements;
    std::string *first_free;
    std::string *cap;
    };
    #endif // STRVEC_H
    view raw StrVec.h hosted with ❤ by GitHub

    #include "StrVec.h"
    void StrVec::reserve(const std::size_t &sz)
    {
    if (sz > capacity())
    {
    auto new_elem = alloc.allocate(sz);
    auto new_p = new_elem, p = elements;
    while (p != first_free)
    alloc.construct(new_p++, std::move(*p++));
    free();
    elements = new_elem;
    first_free = new_p;
    cap = new_elem + sz;
    }
    }
    void StrVec::resize(const std::size_t &sz, const std::string &s = "")
    {
    if (sz > capacity())
    {
    auto new_elem = alloc.allocate(sz * 2);
    auto new_p = new_elem, p = elements;
    while (p != first_free)
    alloc.construct(new_p++, std::move(*p++));
    std::uninitialized_fill_n(new_p, sz - size(), s);
    free();
    elements = new_elem;
    first_free = new_elem + sz;
    cap = new_elem + sz * 2;
    }
    else if (sz > size())
    {
    std::uninitialized_fill_n(first_free, sz - size(), s);
    first_free = elements + sz;
    }
    else
    {
    std::string *new_ff = elements + sz;
    for (auto p = new_ff; p != first_free; ++p)
    alloc.destroy(p);
    }
    }
    void StrVec::free()
    {
    if (elements)
    {
    for (auto p = elements; p != first_free; ++p)
    alloc.destroy(p);
    alloc.deallocate(elements, capacity());
    }
    }
    void StrVec::reallocate()
    {
    std::size_t new_cap = (elements == cap) ? 1 : 2 * size();
    auto new_elem = alloc.allocate(new_cap);
    auto new_p = new_elem, p = elements;
    while (p != first_free)
    alloc.construct(new_p++, std::move(*p++));
    free();
    elements = new_elem;
    first_free = new_p;
    cap = new_elem + new_cap;
    }
    view raw wrong code hosted with ❤ by GitHub

    第二个是StrVec.cc
    顺便求吐槽其他可以改进或改正的地方。
    第 1 条附言  ·  2015-05-19 09:14:46 +08:00
    手机端看不了代码啊。。。
    第 2 条附言  ·  2015-05-19 09:15:17 +08:00
    呃,能看
    第 3 条附言  ·  2015-05-19 12:26:03 +08:00
    问题解决了
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3544 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 10:41 · PVG 18:41 · LAX 03:41 · JFK 06:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.