我真的不確定爲什麼我收到此錯誤。我試圖谷歌,但我還沒有達到最佳效果的......如果有人能告訴我,爲什麼我得到這個錯誤:爲什麼我得到「沒有可行的轉換從'矢量<Country>'到'int'」?
No viable conversion from 'vector<Country>' to 'int'
int main()
{
vector<Country> readCountryInfo(const string& filename);
// Creating empty vector
vector<Country> myVector;
// Opening file
ifstream in;
in.open("worldpop.txt");
if (in.fail()) {
throw invalid_argument("invalid file name");
}
while (in) {
char buffer; // Character buffer
int num; // Integer to hold population
string countryName; // Add character buffer to create name
while (in.get(buffer)) {
// Check if buffer is a digit
if (isdigit(buffer)) {
in.unget();
in >> num;
}
// Check if buffer is an alphabetical character
else if (isalpha(buffer) || (buffer == ' ' && isalpha(in.peek()))) {
countryName += buffer;
}
// Checking for punctuation to print
else if (ispunct(buffer)) {
countryName += buffer;
}
// Check for new line or end of file
else if (buffer == '\n' || in.eof()) {
// Break so it doesn't grab next char from inFile when running loop
break;
}
}
Country newCountry = {countryName, num};
myVector.push_back(newCountry);
}
return myVector;
}
哪一行是錯誤來自哪裏? – WiSaGaN 2014-09-29 01:25:06
爲什麼你的主要返回'vector'? –
humodz
2014-09-29 01:25:44
爲什麼不使用CSV文件的名稱和人口按約定的順序,而不是嘗試猜測每個字符的基礎? – 2014-09-29 01:26:24