我不知道在我的代码中导致分段错误的原因

时间:2016-09-26 23:35:56

标签: c++ segmentation-fault

我的机智结束了这段代码,我是c ++的新手,所以我甚至不知道从哪里开始检查这个错误。在我的代码中导致分段错误的原因是什么?如何解决?

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

class Student
{
  private:
    string name;
    int numClasses;
    vector<string> classList;

  public:
    Student (string nameVal, int numClassesVal, string classListArr[]);
    Student (string nameVal, int numClassesVal);
    Student (string nameVal);
    Student ();
    void setName(string nameVal);
    void setNumClasses(int numClassesVal);
    void setClassList(string classListArr[]);
    string getName();
    int getNumClasses();
    vector<string> getClassList();
    void input();
    void output();
    void reset();
};



int main ()
{
  Student user;
  string answer;
  int flag = 1;

  while (flag == 1)
  {
    user.input();
    user.output();

    cout << "Do you want to enter the new data? (y/n)" << endl;
    cin >> answer;
    cin.ignore();

    if (answer == "n")
    {
      flag == 0;
    }
  }

  return 0;
}

Student::Student (string nameVal, int numClassesVal, string classListArr[])
  : name(nameVal), numClasses(numClassesVal)
{
  string test = classListArr[0];
  for (int x = 0; x < 10; ++x)
  {
    if ( (x > 0) && (classListArr[x] == test))
      break;
    classListArr[x] = classList[x];
  }
}

Student::Student (string nameVal, int numClassesVal)
{
  name = nameVal;
  numClasses = numClassesVal;

  for (int x = 0; x < 10; ++x)
  {
    classList[x] = "";
  }
}

Student::Student (string nameVal)
{
  name = nameVal;
  numClasses = 0;

  for (int x = 0; x < 10; ++x)
  {
    classList[x] = "";
  }
}

Student::Student ()
{
  name = "";
  numClasses = 0;

  for (int x = 0; x < 10; ++x)
  {
    classList[x] = "";
  }
}


void Student::setName(string nameVal) {name = nameVal;}

void Student::setNumClasses(int numClassesVal) {numClasses = numClassesVal;}

void Student::setClassList(string classListArr[]) 
{
  string test = classListArr[0];
  for (int x = 0; x < 10; ++x)
  {
    if ( (x > 0) && (classListArr[x] == test))
      break;
    classListArr[x] = classList[x];
  }
}

string Student::getName() {return name;}

int Student::getNumClasses() {return numClasses;}

vector<string> Student::getClassList() {return classList;}

void Student::input()
{
  string temp;
  int classes;
  cout << "Input Student's Name:" << endl;
  getline(cin, temp);
  name = temp;
  cout << "Input Number of Classes:" << endl;
  cin >> classes;
  cin.ignore();
  numClasses = classes;

  for (int x = 0; x < 10; ++x)
  {
    if ( x == 9)
    {
      cout << "Input Last Class Name (Maximum of 10):";
      getline(cin, temp);
      classList[9] = temp;
    }
    else
    {
      cout << "Input Class Name:";
      getline(cin, temp);
      classList[x] = temp;
      cout << "Do you have any more classes? (y/n):";
      getline(cin, temp);

      if ( temp == "n")
        break;
    }
  }

}

void Student::output()
{
  cout << "Student Name: " << name << "\nNumber of Classes: " << numClasses << endl;


 cout << "Names of Classes: " << endl;

  for (int x = 0; x < 10; ++x)
  {
    if ( numClasses == 0 )
      break;
    if ( x == 0 )
      cout << "Names of Classes: \n";
    if ( classList[x] == "" )
      break;

    cout << "(" << x << ") " << classList[x] << "\n";
  }
}

void Student::reset()
{
  numClasses = 0;
  for (int x = 0; x < 10; ++x)
  {
    classList[x] = "";
  }
}

我尝试更改主函数,之前我有一个goto语句而不是一个标志,并且没有修复它。我认为它是cin.getline(),但我不确定。

1 个答案:

答案 0 :(得分:1)

您在此代码(以及其他构造函数)中有未定义的行为:

Student::Student ()
{
  name = "";
  numClasses = 0;

  for (int x = 0; x < 10; ++x)
  {
    classList[x] = "";
  }
}

您从未在classList向量中分配任何元素,因此对classList[x]的引用超出范围。您应该使用.push_back()向向量添加新元素:

Student::Student ()
{
  name = "";
  numClasses = 0;

  for (int x = 0; x < 10; ++x)
  {
    classList.push_back("");
  }
}

或者在循环之前,您可以classList.reserve(10);为10个元素腾出空间。

请参阅C++ Tutorial: A Beginner's Guide to std::vector