为什么我会遇到未解决的外部问题?

发布于 2024-08-07 13:14:01 字数 2282 浏览 3 评论 0原文

我正在用 C++ 编写一个不可变的二叉搜索树。我的终止节点由单个空节点表示。我的编译器(Visual C++)似乎在解析保存我的单例的受保护静态成员时遇到问题。我收到以下错误:

error LNK2001: unresolved external symbol "protected: static class boost::shared_ptr > node::m_empty" (?m_empty@?$node@HH@@1V?$shared_ptr@V?$node@HH @@@boost@@A)

我假设这意味着它无法解析类型节点的静态 m_empty 成员。这是正确的吗?如果是这样我该如何修复它?

代码如下:

using namespace boost;
template<typename K, typename V>
class node {
protected:
    class empty_node : public node<K,V> {
    public:
        bool is_empty(){ return true; }
        const shared_ptr<K> key() { throw cant_access_key; }
        const shared_ptr<V> value()  { throw cant_access_value; }
        const shared_ptr<node<K,V>> left()  { throw cant_access_child; }
        const shared_ptr<node<K,V>> right()  { throw cant_access_child; }
        const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value){
            return shared_ptr<node<K,V>>();
        }
        const shared_ptr<node<K,V>> remove(const shared_ptr<K> &key) { throw cant_remove; }
        const shared_ptr<node<K,V>> search(const shared_ptr<K> &key) { return shared_ptr<node<K,V>>(this); }
    };

    static shared_ptr<node<K,V>> m_empty;
public:
    virtual bool is_empty() = 0;
    virtual const shared_ptr<K> key() = 0;
    virtual const shared_ptr<V> value() = 0;
    virtual const shared_ptr<node<K,V>> left() = 0;
    virtual const shared_ptr<node<K,V>> right() = 0;
    virtual const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value) = 0;
    virtual const shared_ptr<node<K,V>> remove(const shared_ptr<K> &key) = 0;
    virtual const shared_ptr<node<K,V>> search(const shared_ptr<K> &key) = 0;


    static shared_ptr<node<K,V>> empty() {
        if(m_empty.get() == NULL){
            m_empty.reset(new empty_node());
        }
        return m_empty;
    }
};

我的树的根初始化为:

shared_ptr<node<int,int>> root = node<int,int>::empty();

I am writing an immutable binary search tree in c++. My terminating nodes are represented by a singleton empty node. My compiler (visual c++) seems to be having trouble resolving the protected static member that holds my singleton. I get the following error:

error LNK2001: unresolved external symbol "protected: static class boost::shared_ptr > node::m_empty" (?m_empty@?$node@HH@@1V?$shared_ptr@V?$node@HH@@@boost@@A)

I am assuming this means it cant resolve the static m_empty member for the type node. Is this correct? If so how do I fix it?

Code follows:

using namespace boost;
template<typename K, typename V>
class node {
protected:
    class empty_node : public node<K,V> {
    public:
        bool is_empty(){ return true; }
        const shared_ptr<K> key() { throw cant_access_key; }
        const shared_ptr<V> value()  { throw cant_access_value; }
        const shared_ptr<node<K,V>> left()  { throw cant_access_child; }
        const shared_ptr<node<K,V>> right()  { throw cant_access_child; }
        const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value){
            return shared_ptr<node<K,V>>();
        }
        const shared_ptr<node<K,V>> remove(const shared_ptr<K> &key) { throw cant_remove; }
        const shared_ptr<node<K,V>> search(const shared_ptr<K> &key) { return shared_ptr<node<K,V>>(this); }
    };

    static shared_ptr<node<K,V>> m_empty;
public:
    virtual bool is_empty() = 0;
    virtual const shared_ptr<K> key() = 0;
    virtual const shared_ptr<V> value() = 0;
    virtual const shared_ptr<node<K,V>> left() = 0;
    virtual const shared_ptr<node<K,V>> right() = 0;
    virtual const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value) = 0;
    virtual const shared_ptr<node<K,V>> remove(const shared_ptr<K> &key) = 0;
    virtual const shared_ptr<node<K,V>> search(const shared_ptr<K> &key) = 0;


    static shared_ptr<node<K,V>> empty() {
        if(m_empty.get() == NULL){
            m_empty.reset(new empty_node());
        }
        return m_empty;
    }
};

the root of my tree is initialized as:

shared_ptr<node<int,int>> root = node<int,int>::empty();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生来就爱笑 2024-08-14 13:14:01

正如其他人所说,您需要为静态成员提供一个定义点。但是,由于它是模板的成员,因此语法将比之前建议的更复杂一些。如果我没有遗漏任何内容,它应该如下所示

template<typename K, typename V> shared_ptr<node<K,V> > node<K,V>::m_empty;

。如果需要,您还可以在此声明中提供一个初始化程序(或多个初始化程序)。

As others said, you need to provide a definition point for your static member. However, since it is a member of a template, the syntax is going to be a bit more complex then what was suggested before. If I'm not missing anything, it should look as follows

template<typename K, typename V> shared_ptr<node<K,V> > node<K,V>::m_empty;

You can also provide an initializer (or initializers) in this declaration, if necessary.

悍妇囚夫 2024-08-14 13:14:01

m_empty 是静态的,因此您需要一个源 (.cpp) 文件,其中包含以下内容:

template <typename K, typename V> shared_ptr<node<K,V> > node<K,V>::m_empty;

注意:我原来的答案不正确,并且没有考虑到这是一个模板。这是AndreyT在他的回答中给出的答案;我已使用正确答案更新了此答案,因为这是已接受的答案并显示在页面顶部。请支持 AndreyT 的答案,而不是这个。

m_empty is static and so you'll need to have a source (.cpp) file with something like the following:

template <typename K, typename V> shared_ptr<node<K,V> > node<K,V>::m_empty;

Note: My original answer was incorrect and did not take into account that this was a template. This is the answer that AndreyT gave in his answer; I've updated this answer with the correct answer because this is the accepted answer and appears at the top of the page. Please upvote AndreyT's answer, not this one.

单调的奢华 2024-08-14 13:14:01

您需要在 .cpp 文件中初始化 m_empty 变量。

You need to initialize the m_empty variable in your .cpp file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文