Reputation: 3
I want to make a simple function that takes an int as input, makes a vector filled with string elements, then returns a string corresponding to the index of the element.
Everything seems to be fine with the code, no red squiggly lines or anything, but when I compile nothing happens at all.
I've used the push_back()
method to fill the vector, so that isn't the problem.
#include <iostream>
#include <vector>
#include <string>
races(int a) {
std::vector<std::string> race = { "Human", "Cyborg", "Android", "Aster","Ool-Rog","Criataz" };
return race[a];
};
I've run it on a browser coding site and nothing happens, and when I code it in Visual Studio I get an error:
"cannot find class to resolve delegate string"
Upvotes: 0
Views: 554
Reputation: 305
as per comments by genpfault and Remy Lebeau, here is a working code sample:
#include <iostream>
#include <vector>
#include <string>
std::string races(int a) {
std::vector<std::string> race = { "Human", "Cyborg", "Android", "Aster","Ool-Rog","Criataz" };
return race[a]; // or race.at(a);
};
int main () {
std::cout << "Race with index: " << 2 << " is: " << races(2) << std::endl;
return 0;
}
Example compilation and execution, with results on MAC OSX with C++ 14 installed, in Terminal utilities app:
$ /usr/local/bin/c++-9 races.cpp -o races
$ ./races
Race with index: 2 is: Android
Keep on coding!
Upvotes: 3