ostream的問題 我的ostream操作< <似乎並沒有被工作或者ostream的運營商<<不能正常工作
0
A
回答
2
東西有一個在功能Customer::getHash
一個邏輯錯誤。這可能無法解決您的問題,但無論如何它都應該得到解決。
int Customer::getHash(int hash)
{
string key = getLastname();
cout<<"key: "<<key<<endl;
// getFirstname();
// getID();
int i = 0;
// int j = 0;
// int k = 0;
for (i = 0; i < key.length(); i++)
{
i += (int)key[i]; // Problem.
// At this time, i may be greater than key.length().
}
// getFirstname();
// getID();
return i = i % hash;
}
您可以通過使用其他變量來修復它以保留臨時哈希值。
int Customer::getHash(int hash)
{
string key = getLastname();
cout<<"key: "<<key<<endl;
int tempHash = 0;
int i = 0;
for (i = 0; i < key.length(); i++)
{
tempHash += (int)key[i];
}
return tempHash % hash;
}
更新
在您發佈的代碼,您在功能
istream &operator >> (istream &in, Customer &obj)
作爲一個副作用註釋掉return語句的
while (inputFile >> newCustomer)
行爲
未定義。
取消註釋在該函數的線
//return in;
。這將解決另一個錯誤。希望這是最後一個。
更新2
您正在閱讀的while
循環太多的信息。
// This line reads all the information of one customer
while (inputFile >> newCustomer)
{
//inputFile >> newCustomer;
string lastname;
// PROBLEM
// Now you are reading data corresponding to the next customer.
getline (inputFile, lastname, ' ');
while (inputFile.peek() == ' ')
inputFile.get();
string firstname;
getline (inputFile, firstname, ' ');
while (inputFile.peek() == ' ')
inputFile.get();
string id;
getline (inputFile, id);
buildCustomerList(cHeadPtr, cTailPtr, lastname, firstname, id);
customer.insert(newCustomer);
//cout<<lastname<<endl;
//cout<<firstname<<endl;
//cout<<id<<endl;
}
將其更改爲:
while (inputFile >> newCustomer)
{
string lastname = newCustomer.getLastname();
string firstname = newCustomer.getFirstname();
string id = newCustomer.getID();
buildCustomerList(cHeadPtr, cTailPtr, lastname, firstname, id);
customer.insert(newCustomer);
}
相關問題
- 1. ostream的deferencing運營商<<
- 2. 運營商<<(ostream&os,...)模板類
- 3. ostream的運營商<<調用父ostream的
- 4. JavaScript運營商+不能正常工作
- 5. 支持中的std :: ostream的運營商<<
- 6. ostream的運營商<<對地址和指向
- 7. 印刷使用的ostream和運營商<<
- 8. 運營商<<超載
- 9. 超載運營商<<
- 10. 運營商<<重載
- 11. 虛擬<<運營商
- 12. 運營商<<在C#
- 13. Ruby:<< - 運營商
- 14. 全球運營商<<和成員運營商之間的共存<<
- 15. 朋友ostream的運營商<<不會存取權限私人領域
- 16. 錯誤不符合運營商<<
- 17. 重載運營商的std :: ostream的和運營商<<打印實例內存地址
- 18. stringstream的運營商<<崩潰(malloc_consolidate)
- 19. 運營商的解釋<< overload
- 20. 的Javascript運營商<< and >>
- 21. ActiveRecord的鏟運營商<<
- 22. 運營商<<(ostream的和,常量BigUnsigned <I>&)必須只有一個參數
- 23. 運營商<<(ostream的和,X)類X嵌套在一個類模板
- 24. Angular 1服務 - RxJS共享運營商不能正常工作
- 25. <<運營商不計算表達式正確
- 26. isalpha功能和<=運營商
- 27. 運營商<<重載在C++中
- 28. 運營商<< and >>重載
- 29. 模板運營商<<變圓了
- 30. <<運營商紅寶石
嗯,我會想象(INT)鍵[I]可以給你,因爲大小不匹配的垃圾數據... – hanshenrik 2015-02-23 19:40:00
我的尺寸爲512,大小進入getHash的是512.你會建議什麼?謝謝 – kris 2015-02-23 19:44:39