我有一個包含string
字段的結構。我創建了這些結構的數組,然後我想將它們傳遞給一個函數(通過引用)。當我註釋掉string
字段時,一切正常,但如果我沒有程序崩潰。我無法找到一個答案的任何地方..無法在包含字符串的結構數組上運行C++
下面的代碼(我把它降低到只顯示問題):
struct student {
int a;
int b;
string name[20];
char status;
};
void operation(student the_arr[1],int number_of_students) {
delete[] the_arr;
the_arr = new student[3];
for(int i = 0; i<3; i++) {
the_arr[i].a = i+5;
the_arr[i].b = i+4;
}
}
int main() {
student *abc;
abc = new student[0];
operation(abc, 0);
system("pause");
return 0;
}
我需要的數組是動態的,這樣我就可以改變它的大小,當我需要。
你應該使用一個向量。 – chris 2013-03-22 16:30:19
是的,使用一個像矢量一樣的C++容器。 – 2013-03-22 16:30:37
'new student [0]','void operation(student the_arr [1],...':WAT? – us2012 2013-03-22 16:31:02