嗨我的头文件中有一个数组对象。
private:
Customer** customerListArray;
然后在我的CPP文件中,我设置数组大小并添加如下对象,
void AppManager::createArrays(vector<vector<string> > data) {
customerListArray = new Customer* [data.size()];
for (unsigned int i = 0; i < data.size(); i++){
try {
Customer* cust = createCustomerObject(data[i]);
customerListArray[i] = cust;
}
catch(string err) {
cout << "Error processing line '" << i << "' of data file. Error Message : " << err << "\n";
}
}
}
数据向量的大小约为14000项。但是,当我遍历customerListArray并打印项目时,我收到以下错误,
C5545 - Burton G. Craft - 3/1/14
C5546 - Zena F. Wallace - 10/2/14
C5547 - Ursa P. Orr - 20/4/14
C5548 - Adara X. Rogers - 3/6/13
C5549 - Carter L. Newman - 15/1/14
C5550 - Maxwell V. Watkins - 5/6/13
C5551 - Ria V. Morrow - 8/12/12
C5552 - Kirk Z. Hines - 27/4/14
C5553 - Brent L. Bentley - 13/10/13
C5554 - Thane W. Cobb - 8/8/14
C5555 - Serina M. Macias - 13/8/13
C5556 - Maryam J. Lynch - 1/6/13
C5557 - Breanna M. Lewis - 20/10/13
Segmentation fault
它始终在C5557项目中因分段故障而崩溃。当我使用矢量时,它可以正常工作。但我的要求是通过作业使用数组。
请告知:)
谢谢。
编辑:
vector<vector<string> > data= fileReader(customerFile);
data= customerVals.size(); //about 14000 records
//debug priting
for (unsigned int i = 0; i < numOfCustomers; i++) {
Customer* cust = customerListArray[i];
cout << cust->getCustomerId() << " - " << cust->getName() << " - " << cust->getDateLastPurchased()->getDay() << "/" << cust->getDateLastPurchased()->getMonth() << "/" << cust->getDateLastPurchased()->getYear() << "\n";
}
答案 0 :(得分:0)
做&#39; H!我忘了检查null,非常感谢,元素5558永远不会因为异常而被添加到数组中,因此它是空的!
for (unsigned int i = 0; i < numOfCustomers; i++) {
Customer* cust = customerListArray[i];
if(cust != NULL) {
cout << cust->getCustomerId() << " - " << cust->getName() << " - " << cust->getDateLastPurchased()->getDay() << "/" << cust->getDateLastPurchased()->getMonth() << "/" << cust->getDateLastPurchased()->getYear() << "\n";
}
}
检查NULL修复了它的问题!