c++ - 从文件段错误(核心转储)中读取字符串

标签 c++ string constructor stream

class BPP
{
    unsigned            n;                  /* nº de instancias */
    vector<string>      nombre_instancia;   /* nombre de la instancia*/

在构造函数中,出现段错误(核心已转储)时:

file1.open(fich1);
    if (file1.is_open()){
        file1 >> (unsigned &) n;
        for (unsigned k = 0 ; k < n ; ++k){
            getline(file1, nombre_instancia[k]); #gives the segmentation fault

fich1 中的前两行是:

10
 P_0

最佳答案

我猜你没有将 nombre_instancia 的大小调整到超过其原始大小 0。试试这个:

  file1 >>  n;
  nombre_instancia.resize(n);     
    for (unsigned k = 0 ; k < n ; ++k){
        getline(file1, nombre_instancia[k]); #gives the segmentation fault

关于c++ - 从文件段错误(核心转储)中读取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018652/

相关文章:

c++ - 类派生形式结构上的虚函数

c++ - boost managed_mapped_file : setting maximum allowed memory usage

Java字符串格式错误

c# - 为什么我要使用托管(C# 和 .NET)或 native 代码进行 Windows API 开发?

java - 为什么这个正则表达式没有给出预期的输出?

Java String的split方法忽略空子串

java - 在具有指定容量的构造函数中初始化 linkedList 大小的最佳选项是什么?

java - 如何在 Java 中使用泛型获取反射构造函数?

c++ - 就地重新初始化对象

c++ - 错误 C2106 : '=' : left operand must be l-value in Fibonacci sequence by dynamic programming in C++