I'm really not sure as to why I am receiving this error. I've tried to google it but I haven't had the best of results... If somebody could just tell me as to why I'm getting this error:
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;
}
It says here
int main()
that main
returns an int
— as it should, because The Standard requires it to.
Then, at the end, you say
return myVector;
and myVector
is a vector<Country>
, which can't be converted to an int
.
Hence the error message.
I suspect, based on the declaration
vector<Country> readCountryInfo(const string& filename);
of a function that does return a vector<Country>
, that you intended to write your code in a function called "readCountryInfo", but somehow happened to write it in the wrong place.
Your int main()
should return an int, not myVector
(last line of your code).
In c++, main returns an int, normally zero.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.