Can ANYTHING be inserted into STL vector?

Z

Zheka

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 :(
 
P

Peter van Merkerk

[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.
 
J

John Harrison

Zheka said:
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
 
H

Howard

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
 
J

John Harrison

Howard said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top