2014-09-29 19 views
0

我真的不確定爲什麼我收到此錯誤。我試圖谷歌,但我還沒有達到最佳效果的......如果有人能告訴我,爲什麼我得到這個錯誤:爲什麼我得到「沒有可行的轉換從'矢量<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; 

} 
+0

哪一行是錯誤來自哪裏? – WiSaGaN 2014-09-29 01:25:06

+4

爲什麼你的主要返回'vector '? – humodz 2014-09-29 01:25:44

+0

爲什麼不使用CSV文件的名稱和人口按約定的順序,而不是嘗試猜測每個字符的基礎? – 2014-09-29 01:26:24

回答

5

它說這裏

int main() 

main返回一個int - 因爲它應該,因爲標準要求它。

然後,在結束時,你說

return myVector; 

myVectorvector<Country>,它不能被轉換爲int
因此,錯誤消息。

我懷疑的基礎上,申報

vector<Country> readCountryInfo(const string& filename); 
一個函數, 確實返回 vector<Country>,你打算寫你的代碼在一個名爲「readCountryInfo」功能的

,但不知何故發生了寫在錯誤的地方。

+0

非常感謝你......愚蠢的錯誤 – 2014-09-29 01:52:16

1

你的int main()應該返回一個int,而不是myVector(你的代碼的最後一行)。

在C++中,主要返回一個int,通常爲零。

相關問題