Can ANYTHING be inserted into STL vector?

Discussion in 'C++' started by Zheka, Jul 30, 2003.

  1. Zheka

    Zheka Guest

    we're dealing the following problem:

    d:\Program Files\Microsoft Visual Studio .NET\Vc7\include\vector(575):
    error C2440: 'initializing' : cannot convert from 'const Game' to
    'Game'


    This error comes because of the following:

    Game newGame(numPlayers, playerNames, boardKind);
    games.insert(games.end(), newGame);


    games is a vector <Game>

    and Game is:

    class Game
    {
    public:
    Game(const int numPlayers, const vector <string> playerNames, bool
    boardKind);
    void playOneRound();

    private:
    vector <Player> players;
    Board board;
    Heap heap;
    Dictionary dict;
    };


    We began getting this error only after the field Dictionary dict was
    added. The class Dictionary includes ifstream dictionaryFile;

    Could this cause this problem?? What could this be?
    Please help :(
     
    Zheka, Jul 30, 2003
    #1
    1. Advertising

  2. [snip]

    > We began getting this error only after the field Dictionary dict was
    > added. The class Dictionary includes ifstream dictionaryFile;
    >
    > Could this cause this problem?? What could this be?


    To answer the question in the subject line: No, object must be copy
    constructable and assignable if they are to be used in standard
    containers. If your class does not meet these criteria it cannot be used
    (directly) in a standard containers.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Jul 30, 2003
    #2
    1. Advertising

  3. "Zheka" <> wrote in message
    news:...
    > we're dealing the following problem:
    >
    > d:\Program Files\Microsoft Visual Studio .NET\Vc7\include\vector(575):
    > error C2440: 'initializing' : cannot convert from 'const Game' to
    > 'Game'
    >
    >
    > This error comes because of the following:
    >
    > Game newGame(numPlayers, playerNames, boardKind);
    > games.insert(games.end(), newGame);
    >
    >
    > games is a vector <Game>
    >
    > and Game is:
    >
    > class Game
    > {
    > public:
    > Game(const int numPlayers, const vector <string> playerNames, bool
    > boardKind);
    > void playOneRound();
    >
    > private:
    > vector <Player> players;
    > Board board;
    > Heap heap;
    > Dictionary dict;
    > };
    >
    >
    > We began getting this error only after the field Dictionary dict was
    > added. The class Dictionary includes ifstream dictionaryFile;
    >
    > Could this cause this problem?? What could this be?
    > Please help :(


    Not anything can be inserted into a vector. Any STL class must be Copy
    Constructible and Assignable. ifstream is neither, so anything that includes
    an ifstream cannot be inserted in a vector, unless you write your own copy
    constructor and assignment operator for Dictionary.

    Suggest you do this but consider very carefully what it means to copy an
    ifstream object. How can you have two Dictionary objects using the same
    file? Will you open the same file twice (not very efficient). Or maybe you
    will have a pointer to the file, so two Dictionaries will have a pointer to
    the same ifstream, but then how do you know when to close the file, you will
    have to count the number of pointers. In short you have some thinking to do,
    and some learning, find a book that discusses smart pointers, they are
    probably the answer to this particular problem.

    John
     
    John Harrison, Jul 30, 2003
    #3
  4. Zheka

    Howard Guest

    He could solve this by using a pointer to a Game object, right? As in:

    Game* pNewGame = new Game(...);
    games.insert(games.end(), pNewGame);
    ....(and of course deleting it later)

    Of course, that doesn't address the issue of the ifstream possibly being
    used more that once, but it solves the insertion problem, doesn't it?

    -Howard
     
    Howard, Jul 30, 2003
    #4
  5. "Howard" <> wrote in message
    news:bg8l9d$...
    >
    > He could solve this by using a pointer to a Game object, right? As in:
    >
    > Game* pNewGame = new Game(...);
    > games.insert(games.end(), pNewGame);
    > ...(and of course deleting it later)
    >
    > Of course, that doesn't address the issue of the ifstream possibly being
    > used more that once, but it solves the insertion problem, doesn't it?
    >
    > -Howard
    >


    Yes but only at the cost of introducing a bigger problem, how to track
    possible multiple copies of pointers to dynamically allocated memory.

    I was very impressed that the OP didn't have a single pointer in the posted
    code. My preference would be to use a smart pointer to wrap the uncopyable
    ifstream object. Then the OP can use the original pointerless code.

    But of course only the OP really knows what s/he requires. Maybe a vector of
    Game pointers is a reasonable solution, it all depends on how that vector
    would be used.

    John
     
    John Harrison, Jul 30, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    24
    Views:
    1,697
    alchemist
    Aug 4, 2005
  2. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
  3. zl2k
    Replies:
    27
    Views:
    1,587
    Francesco S. Carta
    Sep 7, 2010
  4. Michel
    Replies:
    1
    Views:
    82
    Thomas 'PointedEars' Lahn
    Oct 23, 2003
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    375
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page