我使用这个简单的函数来创建一个新的节点在以下程序中导致运行时错误的原因是什么?
node* Tree::createNewNode(int score, const char* word)
{
// Create a new node with the information available
node* n = new node;
n->left=NULL;
n->right = NULL;
n->parent = NULL;
n->score = score;
strcpy(n->word,word);
return n;
}
节点的结构:
struct node
{
int score; // the score or label of the node
char *word; // the word stored in the node
node *left; // the pointer to left child of the node
node *right; // the pointer to right child of the node
node *parent; // the pointer to parent node
};
,我来自另一个函数调用createNewNode功能
temp = t->createNewNode(score,"");
的函数只能正常运行一次,然后在执行时崩溃:
node* n = new node;
我建议不要遵循给出的两个答案中的建议,并使用'std :: string'和构造函数而不是传统的C函数。 – TartanLlama