std :: list(双向链表)未知运行时错误

时间:2018-04-03 04:49:23

标签: c++ string list nodes std

我在操作链表时遇到问题。我以前能够做到这一点,但无法弄清楚为什么我得到一个运行时错误只是说“程序已经停止工作,没有解释。无论我使用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;
}

1 个答案:

答案 0 :(得分:1)

_words是指向std::list的指针。你在构造函数中将它设置为NULL,而不是其他任何东西。它看起来不像是一个指针,只是直接作为tnode的成员。