 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Zheka Guest
|
Posted: Wed Jul 30, 2003 2:00 pm Post subject: Can ANYTHING be inserted into STL vector? |
|
|
we're dealing the following problem:
d:Program FilesMicrosoft Visual Studio .NETVc7includevector(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
|
|
| Back to top |
|
 |
Peter van Merkerk Guest
|
Posted: Wed Jul 30, 2003 2:17 pm Post subject: Re: Can ANYTHING be inserted into STL vector? |
|
|
[snip]
| Quote: | 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
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Wed Jul 30, 2003 2:26 pm Post subject: Re: Can ANYTHING be inserted into STL vector? |
|
|
"Zheka" <zheka (AT) netvision (DOT) net.il> wrote
| Quote: | we're dealing the following problem:
d:Program FilesMicrosoft Visual Studio .NETVc7includevector(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
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
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Wed Jul 30, 2003 2:38 pm Post subject: 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(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
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Wed Jul 30, 2003 4:58 pm Post subject: Re: Can ANYTHING be inserted into STL vector? |
|
|
"Howard" <alicebt (AT) hotmail (DOT) com> wrote
| Quote: |
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|