1

当我运行我的程序时,我得到了 Segmentation Fault 错误。我相信它来自我如何使用在类定义中私下声明的数组“string *words”。我在 .cpp 文件中使用它,有人知道我需要更改什么吗?继承人我认为问题出在的功能:

Dictionary::Dictionary(string filename){

    ifstream inF;

    inF.open(filename.c_str());

    if (inF.fail()){
      cerr << "Error opening file" <<endl;
      exit(1);
    }

    inF >> numwords;
    numwords = 3000;
    words = new string(words[numwords]);


    for(int i=0; i <= numwords - 1; i++){
      inF >> words[i];
    }
    inF.close();
  }
4

2 回答 2

9

该行:

words = new string(words[numwords]);

实际上应该是:

words = new string[numwords];
于 2011-04-19T00:18:39.817 回答
4

您将需要学习如何使用调试器。确切的程序取决于您使用的系统。但是,如果您在调试器中运行代码,调试器将在检测到问题的确切行处停止。可以想象,这对调试很有帮助。

请注意,检测到问题的行和问题开始的行可能完全不同。

于 2011-04-19T00:19:08.057 回答