0
我已經通過各種論壇帖子看過這裏和其他網站,但我還沒有看到任何涉及類似問題的內容。我遇到的問題是:除了數組元素爲6或以下時,studentInfo數組將不能正常運行。 我只是希望能夠有一個大小爲23數組,但是代碼返回:在類中使用數組時遇到問題(C++)
bash: line 12: 51068 Segmentation fault $file.o $args
我下面提供的代碼是我的實際代碼的簡化版本。我必須在我的程序中使用一個數組(因爲有些人可能會建議,所以沒有矢量),因爲它是我的任務的一部分。我對C++還是一個新的東西,所以任何答案的好解釋都會很棒。謝謝你的幫助!
#include <iostream>
#include <string>
using namespace std;
class StudentGrades {
private:
string studentInfo[23];
public:
void setStudentInfo(string info) {
for (int i = 0; i < 23; ++i) {
studentInfo[i] = info;
}
}
string getStudentInfo() {
for (int i = 0; i < 23; ++i) {
cout << studentInfo[i] << " ";
}
}
};
int main() {
StudentGrades student1;
student1.setStudentInfo("Bob");
student1.getStudentInfo();
}
它的簡化程度如何?一個問題是getStudentInfo不返回一個字符串。 –