lundi 21 janvier 2019

Trouble returning a single string from a collection fo strings

I'm stumped. I have this class.

class BunnyNameGenerator

std::vector<std::string> m_names;
public:
bool LoadNames(const char* filename);
const std::srting& GetRandomName() const;

My issue is that after I LoadName(); like so

bool BunnyNameGenerator::LoadNames(const char* filename)
{

ifstream file ("BunnyNames.txt");
string name;
if(file.is_open())
{
    while (getline(file, name))
    {
        m_names.push_back(name);

    }

}
else
{
    cout << "Unable to open file";
}

return true;

}

I then want to get a random name with GetRandomName();

const std::string& BunnyNameGenerator::GetRandomName() const
{
// TODO: insert return statement here
}

I know I cannot return m_names; as that is a collection of strings and I need to return a single string but I can't seem to figure out how to do so.

Aucun commentaire:

Enregistrer un commentaire