Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Moving from MFC CList to stl::vector; please help

Moving from MFC CList to stl::vector; please help

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestioncsharp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cloaca
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • C Cloaca

      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

      C Offline
      C Offline
      Cloaca
      wrote on last edited by
      #2

      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 regarding tempVector at the end of the copy constructor code? Thanks very much again, :) Eric

      D 1 Reply Last reply
      0
      • C Cloaca

        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 regarding tempVector at the end of the copy constructor code? Thanks very much again, :) Eric

        D Offline
        D Offline
        Diddy
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • D Diddy

          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

          C Offline
          C Offline
          Cloaca
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • C Cloaca

            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

            D Offline
            D Offline
            Diddy
            wrote on last edited by
            #5

            No problem :) Hope it works

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups