Moving from MFC CList to stl::vector; please help
-
Hi there everyone, I'll try to be brief in describing my difficulty. I have been given good advice from this forum that it would be good to learn how to use the STL instead of always using MFC collections like CList. I have been convinced; seeing how many useful tools are available (I'm going to be using stable_sort for example). Here is the deal. My App has a class (CMyDBM) that had as a member a CList as follows:
CList<CPlayerRec*, CPlayerRec*> m_PlayerList;
Where CPlayerRec is a class that itself holds a fixed number of CStrings and some ints (it has no other classes or structs and nothing dynamic, just some CStrings and ints). I then had a series of functions in CMyDBM that did various things. Among them was the copy constructor function. The project compiled fine at that point. I then removed the CList member from CMyDMB and subsituted the following member:std::vector<CPlayerRec*> m_Players;
I changed the copy constructor to the following:CMyDBM::CMyDBM(const CMyDBM& initDBM)
{
CPlayerRec* ptrPlayerRec = NULL;//\*\*\*\*The following line is number 64 in the file (where the error is). std::vector<CPlayerRec\*>::iterator it = initDBM.m\_Players.begin(); for(/\*init is above\*/ ; it != initDBM.m\_Players.end(); it++) { ptrPlayerRec = new CPlayerRec; \*ptrPlayerRec = \*((CPlayerRec\*)(\*it)); m\_Players.push\_back(ptrPlayerRec); }
}
Now, when I try to build, I get the following error: E:\Program Files\Microsoft Visual Studio\MyProjects\Stats\MyDBM.cpp(64) : error C2440: 'initializing' : cannot convert from 'class CPlayerRec *const * ' to 'class CPlayerRec ** ' Conversion loses qualifiers My idea was to store in the vector a collection of CPlayerRec pointers that I could 'new' as I needed them and then access and delete them later. I am open to storing actual CPlayerRec objects; but then how can I create them 'on the fly' with new and then add them to the vector? I would also have to figure out where / how to delete them. I appreciate you reading this lengthy post and offering any advice. Thanks, Eric
-
Hi there everyone, I'll try to be brief in describing my difficulty. I have been given good advice from this forum that it would be good to learn how to use the STL instead of always using MFC collections like CList. I have been convinced; seeing how many useful tools are available (I'm going to be using stable_sort for example). Here is the deal. My App has a class (CMyDBM) that had as a member a CList as follows:
CList<CPlayerRec*, CPlayerRec*> m_PlayerList;
Where CPlayerRec is a class that itself holds a fixed number of CStrings and some ints (it has no other classes or structs and nothing dynamic, just some CStrings and ints). I then had a series of functions in CMyDBM that did various things. Among them was the copy constructor function. The project compiled fine at that point. I then removed the CList member from CMyDMB and subsituted the following member:std::vector<CPlayerRec*> m_Players;
I changed the copy constructor to the following:CMyDBM::CMyDBM(const CMyDBM& initDBM)
{
CPlayerRec* ptrPlayerRec = NULL;//\*\*\*\*The following line is number 64 in the file (where the error is). std::vector<CPlayerRec\*>::iterator it = initDBM.m\_Players.begin(); for(/\*init is above\*/ ; it != initDBM.m\_Players.end(); it++) { ptrPlayerRec = new CPlayerRec; \*ptrPlayerRec = \*((CPlayerRec\*)(\*it)); m\_Players.push\_back(ptrPlayerRec); }
}
Now, when I try to build, I get the following error: E:\Program Files\Microsoft Visual Studio\MyProjects\Stats\MyDBM.cpp(64) : error C2440: 'initializing' : cannot convert from 'class CPlayerRec *const * ' to 'class CPlayerRec ** ' Conversion loses qualifiers My idea was to store in the vector a collection of CPlayerRec pointers that I could 'new' as I needed them and then access and delete them later. I am open to storing actual CPlayerRec objects; but then how can I create them 'on the fly' with new and then add them to the vector? I would also have to figure out where / how to delete them. I appreciate you reading this lengthy post and offering any advice. Thanks, Eric
Hi all, I made the following change to the code: I removed the following line (the one with the error):
std::vector<CPlayerRec*>::iterator it = initDBM.m_Players.begin();
And I added the following two:std::vector<CPlayerRec*> tempVector = initDBM.m_Players; std::vector<CPlayerRec*>::iterator it = tempVector.begin();
The code now compiles, but I am concerned that I kludged it. Did I just shut the comiler up from bitching or is this the correct solution? If this is correct, then what kind of clean-up do I need to perform regardingtempVector
at the end of the copy constructor code? Thanks very much again, :) Eric -
Hi all, I made the following change to the code: I removed the following line (the one with the error):
std::vector<CPlayerRec*>::iterator it = initDBM.m_Players.begin();
And I added the following two:std::vector<CPlayerRec*> tempVector = initDBM.m_Players; std::vector<CPlayerRec*>::iterator it = tempVector.begin();
The code now compiles, but I am concerned that I kludged it. Did I just shut the comiler up from bitching or is this the correct solution? If this is correct, then what kind of clean-up do I need to perform regardingtempVector
at the end of the copy constructor code? Thanks very much again, :) EricYou have basically casted away const-ness in what you have done there - in a round about way anyway, you make a non-const copy of the vector and then call begin(). Look at initDBM decliration - const CMyDBM& initDBM. The version of begin that returns 'iterator' isn't const - therefore you get errors about calling a non-const method on a const object - or an object within a const object in this case. The version that returns 'const_iterator' is const and is what you should strictly use when your not planing on modifying the objects in the collection anyway. Change it to : std::vector::const_iterator it = initDBM.m_Players.begin(); That should work
-
You have basically casted away const-ness in what you have done there - in a round about way anyway, you make a non-const copy of the vector and then call begin(). Look at initDBM decliration - const CMyDBM& initDBM. The version of begin that returns 'iterator' isn't const - therefore you get errors about calling a non-const method on a const object - or an object within a const object in this case. The version that returns 'const_iterator' is const and is what you should strictly use when your not planing on modifying the objects in the collection anyway. Change it to : std::vector::const_iterator it = initDBM.m_Players.begin(); That should work
-
Diddy, Thanks very much for the reply. I see exactly what you mean. I will try it as soon as I get home where the project is. I appreciate your help! Best, Eric