std :: list(双重链接列表)未知运行时错误

std:: list (doubly linked list) unknown runtime error

本文关键字:列表 运行时错误 链接 未知 list std      更新时间:2023-10-16

我在链接列表上操作困难。我过去已经能够做到这一点,但无法弄清楚为什么我会遇到运行时错误,只是说"该程序已经停止工作,没有解释。我是使用push_back,插入还是任何操作,还是任何操作,到达那条代码,我不知道为什么。这是仅显示用于此特定部分的代码。

#include <iostream>
#include <string>
#include <list>
int main() {
    ttree* tree;
    tree = new ttree(); //ttree object
    tree->insert("a string");//insert a string that has been read in
    // passes to ttree insert method
}
class ttree
{
private:
    tnode* _tnodes;  // pointer to _tnodes
    int _maxDepth;  // max depth allowance
    int _currentDepth; // the depth of current node ( root is depth 1)
}
ttree::ttree() //default ttree constructor
{
    _maxDepth = 5;
    _currentDepth = 1;
    _tnodes = new tnode[26];//create Array of 26 tnodes (A-Z)
    //calls tnode constructor
}
void ttree::insert(string key) {
    int index = key[_currentDepth - 1] - 'A';
    (_tnodes)[index].insert(key, _currentDepth);// passes to tnode insert method
}
class tnode
{
private:
    ttree* _nextLevel;  // pointer to the ttree at the next level
    list<string>* _words;  // store keywords
}
tnode::tnode()
{
    _nextLevel = NULL;
    _words = NULL;
}
bool tnode::insert(string key, int level)
{
    cout << key;
    _words->push_back(key);// no matter what I do with the linked list here
    //it errors out right here
    return true;
}

_wordsstd::list的指针。您将其设置为构造函数中的NULL,而永远不要将其设置为其他任何内容。看起来它不需要是指指针,只需将其直接作为tnode的成员即可。