Can ANYTHING be inserted into STL vector?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zheka

    Can ANYTHING be inserted into STL vector?

    we're dealing the following problem:

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


    This error comes because of the following:

    Game newGame(numPlay ers, playerNames, boardKind);
    games.insert(ga mes.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 :(
  • Peter van Merkerk

    #2
    Re: Can ANYTHING be inserted into STL vector?

    [snip]
    [color=blue]
    > 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?[/color]

    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.merke rk(at)dse.nl


    Comment

    • John Harrison

      #3
      Re: Can ANYTHING be inserted into STL vector?


      "Zheka" <zheka@netvisio n.net.il> wrote in message
      news:4dce4ab0.0 307300600.36861 466@posting.goo gle.com...[color=blue]
      > we're dealing the following problem:
      >
      > d:\Program Files\Microsoft Visual Studio .NET\Vc7\includ e\vector(575):
      > error C2440: 'initializing' : cannot convert from 'const Game' to
      > 'Game'
      >
      >
      > This error comes because of the following:
      >
      > Game newGame(numPlay ers, playerNames, boardKind);
      > games.insert(ga mes.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 :([/color]

      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


      Comment

      • Howard

        #4
        Re: Can ANYTHING be inserted into STL vector?


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

        Game* pNewGame = new Game(...);
        games.insert(ga mes.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


        Comment

        • John Harrison

          #5
          Re: Can ANYTHING be inserted into STL vector?


          "Howard" <alicebt@hotmai l.com> wrote in message
          news:bg8l9d$pnt @dispatch.conce ntric.net...[color=blue]
          >
          > He could solve this by using a pointer to a Game object, right? As in:
          >
          > Game* pNewGame = new Game(...);
          > games.insert(ga mes.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
          >[/color]

          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


          Comment

          Working...