C++ Can';t访问/设置空结构的值
我正在创建一个二叉树模板类,虽然用int初始化BST时从未发生过这个特定的运行时错误,但我还没有解决用字符串初始化BST的问题。错误发生在标记行上C++ Can';t访问/设置空结构的值,c++,struct,null,C++,Struct,Null,我正在创建一个二叉树模板类,虽然用int初始化BST时从未发生过这个特定的运行时错误,但我还没有解决用字符串初始化BST的问题。错误发生在标记行上 #ifndef BST_H #define BST_H #include "BSTInterface.h" template <typename T> class BST : public BSTInterface<T> { public: BST() { root = new Node;
#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
第18行是我将开始->数据设置为空的地方。将start->left和start->right设置为null不会给出生成错误
为了让其他代码(我不允许修改)工作,我必须能够将它们设置为null而不是任意值。任何帮助都将不胜感激
编辑:包括过度最小化的副作用
#include "BST.h"
int main(int argc, char * argv[])
{
BST<std::string> myBST;
myBST.addNode("e");
}
编辑2:BST接口的代码
//**** 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
/****您不能修改此文档****/
#ifndef BST\u接口
#定义BST_接口
#包括
/**具有数据、左、右子指针的二叉树节点*/
模板
类接口
{
公众:
BST接口(空){}
虚拟接口(void){}
/**如果节点添加到BST,则返回true,否则返回false*/
虚拟布尔添加节点(常数T&)=0;
/**如果节点已从BST中删除,则返回true,否则返回false*/
虚拟布尔删除节点(常数T&)=0;
/**如果BST清除了所有节点,则返回true,否则返回false*/
虚拟布尔clearTree()=0;
/**以字符串形式返回BST的级别顺序遍历*/
虚拟std::string toString()const=0;
};
#endif//BST\u接口
我使用以下代码试图重现错误:
#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");
}
#包括
#包括
模板
类接口
{
公众:
BST接口(空){}
虚拟接口(void){}
/**如果节点添加到BST,则返回true,否则返回false*/
虚拟布尔添加节点(常数T&)=0;
/**如果节点已从BST中删除,则返回true,否则返回false*/
虚拟布尔删除节点(常数T&)=0;
/**如果BST清除了所有节点,则返回true,否则返回false*/
虚拟布尔clearTree()=0;
/**以字符串形式返回BST的级别顺序遍历*/
虚拟std::string toString()const=0;
};
模板
类BST:公共BST接口
{
私人:
结构体类型
{
T数据;
节点*左;
节点*右;
};
节点*根;
公众:
BST()
{
根=新节点;
}
bool addNode(const T&newVal,Node*start)
{
开始->数据=newVal;//此处出错
返回true;
}
布尔删除节点(常数T&)覆盖{
抛出std::runtime_错误(“尚未实现”);
}
bool clearTree()覆盖{
抛出std::runtime_错误(“尚未实现”);
}
std::string toString()常量重写{
抛出std::runtime_错误(“尚未实现”);
}
布尔添加节点(常量T&val)覆盖{
返回addNode(val,root);
}
};
int main(int argc,char*argv[])
{
BST myBST;
myBST.addNode(“e”);
}
我无法重现错误(编译得很好)。你能提供完整的代码吗 你需要一个完整的例子。您尚未演示如何使用代码。请与about
root=new Node;root=NULL代码>如果我没有将这两行都包含在中,或者至少以某种方式将root设置为null,那么不可修改的代码(打印函数)将无法正常工作。没有经验能够解决这个问题。永远不要为了让代码神奇地“工作”而引入bug(对root的null赋值)。@TroubledProgrammer如果分配一个新的节点
,将指向它的指针存储在root
,然后立即将NULL
存储在root
中,丢失指向刚分配的新节点的指针。这是一个毫无意义的内存泄漏。分配新节点的目的是什么?只有addNode(const T&newVal)继承了我编写的第二个addNode函数(部分代码我不允许修改)。BST接口中从未提到节点,只有addNode、构造函数和析构函数以及其他一些不相关的函数的存根。经过编辑后得到了这些函数。您能提供给出错误的完整代码吗?没有足够的资源来重现错误,当我填补代码中的空白时,它编译得很好(见上文)。David Schwartz在评论中指出,在将root重置为null之后,我试图通过root访问新节点。我停止了尝试,这就解决了问题。
//**** 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
#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");
}