473,388 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Can ANYTHING be inserted into STL vector?

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
4 2701
[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

"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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
3
by: John Black | last post by:
Hi, I have some code like this, itr = find_if(this->pool.begin(), this->pool.end(), bind2nd(mem_fun_ref(&Pool::isAvailable), make_pair(base, high))); But compiler always complains a lot STL...
3
by: lallous | last post by:
Hello Given this: map<int, mystruct> m; m = item; item.id++; m = item; How can I get a direct refrence to the location of the just inserted item?
6
by: kelvSYC | last post by:
This little bit of seeminly innocent code seems to give me these two errors, all on the line that declares check(). Is there some part of C++ that I'm missing out on? class Condition { public:...
138
by: Ian Boyd | last post by:
i've been thrown into a pit with DB2 and have to start writing things such as tables, indexes, stored procedures, triggers, etc. The online reference is only so helpful. The two pdf manuals are...
4
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual...
4
by: mast2as | last post by:
Hi everyone I need to come up with an efficient solution to add/remove/access parameters to an object. Lets say I create an object "Cart" and at run time based on the user input, lets say we add...
16
by: desktop | last post by:
I have implemented a red-black tree which is used for std::set. In the C++ standard 3 different insert methods are specified for the associative container. But as i see it insert adds an object...
4
by: Kelvin Moss | last post by:
Hi all, Is this code doing any UB by any chance -- #include <map> #include <iostream> #include <vector> #include <string> using namespace std;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.