c++ - 带有字符串数组的段错误c++

标签 c++ arrays segmentation-fault bounds

当我运行我的程序时,我收到了段错误。我相信它来自于我如何使用在类定义中私有(private)声明的数组“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();
  }

最佳答案

行:

words = new string(words[numwords]);

实际上应该是:

words = new string[numwords];

关于c++ - 带有字符串数组的段错误c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5710254/

相关文章:

c++ - 制作结构数组?

c++ - 编译时 Unresolved external symbol 错误

c++ - 如何高效初始化大类对象

c++ - 带菜单的输入验证 C++

python - Numpy 数组创建

c - 如何在 SIGSEGV 处理程序中安全地转储内存。 C、Linux

c++ - 将结构传递给 unordered_map 时出现段错误

c - C中扫描和打印字符串的段错误

c++ - 如何在 C++ 中模拟递归类型定义?

c++ - 要重载哪个运算符才能在 if 语句中使用我的类?