0

So I have a string vector values but I want to convert this vector into a vector of type Game, which is my own custom class. How would I go about this?

I'm trying something like this:

void set_games(vector <string> values){ 
    vector <Game> tmp(values.begin(), values.end()); 
    games = tmp;
}

but it's not working. Any suggestions?

Current error message:

no matching function for call to ‘Game::Game(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)’
./classes/game.h:12: note: candidates are: Game::Game()
./classes/game.h:9: note: Game::Game(const Game&)

Update:

Added Game constructor parameter. Everything working as expected.

3
  • std::transform()? Commented Sep 13, 2013 at 22:16
  • 'its not working' In what way is it not working? Good grief! Commented Sep 13, 2013 at 22:18
  • Hah sorry. I'll update the question. Commented Sep 13, 2013 at 22:19

1 Answer 1

4

Well the error message says it all. You need a constructor for Game that takes a single string argument.

class Game
{
public:
    Game(const std::string& s) { ... }
    ...

Once you've done that you could improve the efficiency of your code by ditching the tmp variable

games.assign(values.begin(), values.end());

Alternatively if another constructor isn't sensible from a design point of view then std::transform as Oli suggested.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to make it clear, that constructor needs to contain all the logic to construct a sensible Game object corresponding to the string you pass. If you leave that constructor empty, you'll get a vector full of empty, useless Games.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.