By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Can ANYTHING be inserted into STL vector?

P: n/a
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 :(
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
[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
Jul 19 '05 #2

P: n/a

"Zheka" <zh***@netvision.net.il> wrote in message
news:4d**************************@posting.google.c om...
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
Jul 19 '05 #3

P: n/a

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
Jul 19 '05 #4

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:bg********@dispatch.concentric.net...

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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.