【问题标题】:Can't access/set values of a null struct无法访问/设置空结构的值
【发布时间】:2019-03-31 04:01:33
【问题描述】:

我正在制作一个二叉树模板类,虽然这个特殊的运行时错误从未发生过用整数初始化 BST,但我还没有解决它用字符串初始化 BST。错误发生在标记线上。

#ifndef BST_H
#define BST_H

#include "BSTInterface.h"

template <typename T>
class BST : public BSTInterface<T>
{
public:
    BST()
    {
        root = new Node;
        root = NULL;
    }
    bool addNode(const T& newVal, Node *start)
    {
        start->data = newVal; // ERROR HERE
        return true;
    }
private:
    struct Node
    {
        T data;
        Node *left;
        Node *right;
    };
    Node *root;
};
#endif

我尝试将 root 的每个值都设置为 null,但我得到了这个构建错误:

BST.h(18): error C2593: 'operator =' is 
ambiguous

第 18 行是我将 start->data 设置为 null 的地方。将 start->left 和 start->right 设置为 null 不会产生构建错误。

我必须能够将这些设置为 null 而不是某个任意值,以便其他代码(我不允许修改)工作。任何帮助将不胜感激。

编辑:包括过度最小化的副作用。

#include "BST.h"

int main(int argc, char * argv[])
{
    BST<std::string> myBST;
    myBST.addNode("e");
}

BST 中的附加函数,实际上是从 main 调用的:

bool addNode(const T& newVal)
{
    return addNode(newVal, root);
}

编辑 2:BSTInterface 的代码

//**** YOU MAY NOT MODIFY THIS DOCUMENT ****/
#ifndef BST_INTERFACE_H
#define BST_INTERFACE_H
#include <string>

/** A binary tree node with data, left and right child pointers */
template<typename T>
class BSTInterface
{
public:
    BSTInterface(void) {}
    virtual ~BSTInterface(void) {}

    /** Return true if node added to BST, else false */
    virtual bool addNode(const T&) = 0;

    /** Return true if node removed from BST, else false */
    virtual bool removeNode(const T&) = 0;

    /** Return true if BST cleared of all nodes, else false */
    virtual bool clearTree() = 0;

    /** Return a level order traversal of a BST as a string */
    virtual std::string toString() const = 0;
};
#endif  // BST_INTERFACE_H

【问题讨论】:

  • 你需要一个完整的例子。你还没有展示你是如何尝试使用你的代码的。
  • your rubber duck讨论root = new Node; root = NULL;
  • 如果我不包含这两行,或者至少以某种方式将 root 设置为 null,则不可修改的代码(打印功能)将无法正常工作。没有经验能够解决这个问题。
  • 永远不要为了让代码神奇地“工作”而引入错误(将 null 赋值给 root)。
  • @TroubledProgrammer 你分配了一个新的Node,将指向它的指针存储在root,然后立即将NULL 存储在root 中,丢失指向新的Node 的指针你只是分配。那是毫无意义的内存泄漏。分配新节点的目的是什么?

标签: c++ struct null


【解决方案1】:

我使用下面的代码尝试重现错误:

#include <string>
#include <stdexcept>

template<typename T>
class BSTInterface
{
public:
    BSTInterface(void) {}
    virtual ~BSTInterface(void) {}

    /** Return true if node added to BST, else false */
    virtual bool addNode(const T&) = 0;

    /** Return true if node removed from BST, else false */
    virtual bool removeNode(const T&) = 0;

    /** Return true if BST cleared of all nodes, else false */
    virtual bool clearTree() = 0;

    /** Return a level order traversal of a BST as a string */
    virtual std::string toString() const = 0;
};

template <typename T>
class BST : public BSTInterface<T>
{
   private:
    struct Node
    {
        T data;
        Node *left;
        Node *right;
    };
    Node *root;
   public:
    BST()
    {
        root = new Node;
    }
    bool addNode(const T& newVal, Node *start)
    {
        start->data = newVal; // ERROR HERE
        return true;
    }
    bool removeNode(const T&) override {
        throw std::runtime_error("Not implemented yet"); 
    }
    bool clearTree() override {
        throw std::runtime_error("Not implemented yet"); 
    }
    std::string toString() const override {
        throw std::runtime_error("Not implemented yet"); 
    }

    bool addNode(const T& val) override {
        return addNode(val, root); 
    }

};

int main(int argc, char * argv[])
{
    BST<std::string> myBST;
    myBST.addNode("e");
}

而且我无法重现错误(它编译得很好)。你能提供完整的代码吗?

【讨论】:

  • 只有 addNode(const T& newVal) 被继承(部分代码我不允许修改)我写的第二个 addNode 函数。 BSTInterface 中从未提及 Node,只有 addNode 的存根、构造函数和析构函数以及其他一些不相关的函数。
  • 编辑有那个。
  • 您能否提供导致错误的完整代码?没有足够的东西来重现错误,当我填补代码中的空白时,它编译得很好(见上文)
  • David Schwartz 在 cmets 中指出的是,在将 root 重置为 null 后,我试图通过 root 访问新节点。我停止尝试这样做并且解决了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多