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. CArray template class

CArray template class

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpc++visual-studioquestion
13 Posts 7 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.
  • L Lost User

    Hi I had a structure which looks like this: 1. struct CMyStruct { 5-6 CString ...; int ...; long ...; 1 CStringArray ...; }; 2. My application is going to generate a lot of instances of CMyStruct; I therefore thought it to be a good idea to use the CArray MFC template class to *manage* my structs. (Adding, deleting etc.) 3. Accordingly, I put the following definition in my dialog header file. #include #include CArrayMyStructArray; 4. After this I began using it in the following manner: CMyStruct myStruct; PopulateMyStruct(&myStruct);//Structure populated MyStructArray.Add(myStruct);//ERROR PRODUCING LINE 5. On compilation i receive the following error: \INCLUDE\afxtempl.h(443) : error C2582: 'CMyStruct' : 'operator =' function is unavailable C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\xlocale(467) : while compiling class-template member function 'void __thiscall CArray::SetAtGrow(int,struct CMyStruct &)' 6. What could I be doing wrong?? Any help would be greatly appreciated.:~ :((

    P Offline
    P Offline
    Pavel Klocek
    wrote on last edited by
    #2

    Imlementation of SetAtGrow method uses an operator =. You need to implement one for your struct/class. Pavel Sonork 100.15206

    1 Reply Last reply
    0
    • L Lost User

      Hi I had a structure which looks like this: 1. struct CMyStruct { 5-6 CString ...; int ...; long ...; 1 CStringArray ...; }; 2. My application is going to generate a lot of instances of CMyStruct; I therefore thought it to be a good idea to use the CArray MFC template class to *manage* my structs. (Adding, deleting etc.) 3. Accordingly, I put the following definition in my dialog header file. #include #include CArrayMyStructArray; 4. After this I began using it in the following manner: CMyStruct myStruct; PopulateMyStruct(&myStruct);//Structure populated MyStructArray.Add(myStruct);//ERROR PRODUCING LINE 5. On compilation i receive the following error: \INCLUDE\afxtempl.h(443) : error C2582: 'CMyStruct' : 'operator =' function is unavailable C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\xlocale(467) : while compiling class-template member function 'void __thiscall CArray::SetAtGrow(int,struct CMyStruct &)' 6. What could I be doing wrong?? Any help would be greatly appreciated.:~ :((

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #3

      does CMyStruct have an "operator =" member ? looks like that's what the compiler wants. have you considered std::vector instead of CArray? overall, std::vector is a nicer way to deal with an array of things. -c


      Alcohol is the anesthesia by which we endure the operation of life. -- George Bernard Shaw

      Smaller Animals Software

      R 1 Reply Last reply
      0
      • C Chris Losinger

        does CMyStruct have an "operator =" member ? looks like that's what the compiler wants. have you considered std::vector instead of CArray? overall, std::vector is a nicer way to deal with an array of things. -c


        Alcohol is the anesthesia by which we endure the operation of life. -- George Bernard Shaw

        Smaller Animals Software

        R Offline
        R Offline
        Richard Lewis
        wrote on last edited by
        #4

        hi there, just wanted to let you'll know: When i remove the CStringArray from the structure it works!

        H 1 Reply Last reply
        0
        • R Richard Lewis

          hi there, just wanted to let you'll know: When i remove the CStringArray from the structure it works!

          H Offline
          H Offline
          Holger Persch
          wrote on last edited by
          #5

          This is because of CStringArray has no "=" operator. Best regards Holger Persch

          R 1 Reply Last reply
          0
          • H Holger Persch

            This is because of CStringArray has no "=" operator. Best regards Holger Persch

            R Offline
            R Offline
            Richard Lewis
            wrote on last edited by
            #6

            OK thats fine, but what happens when I **NEED** a CStringArray in my structure. (Right now i have replaced the CStringArray by a char**) but thats not very elegant!)

            C H 2 Replies Last reply
            0
            • R Richard Lewis

              OK thats fine, but what happens when I **NEED** a CStringArray in my structure. (Right now i have replaced the CStringArray by a char**) but thats not very elegant!)

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #7

              then you need to write an "=" operator for your struct. in it, you'll copy all the data items from the input struct to "this". -c


              Alcohol is the anesthesia by which we endure the operation of life. -- George Bernard Shaw

              Smaller Animals Software

              R 1 Reply Last reply
              0
              • C Chris Losinger

                then you need to write an "=" operator for your struct. in it, you'll copy all the data items from the input struct to "this". -c


                Alcohol is the anesthesia by which we endure the operation of life. -- George Bernard Shaw

                Smaller Animals Software

                R Offline
                R Offline
                Richard Lewis
                wrote on last edited by
                #8

                Do you mean implementing a = operator that copies from one CStringArray into another. P.S:I say this, because i am now using a statically allocated char[][], thereby eliminating the need for copying!

                C 1 Reply Last reply
                0
                • R Richard Lewis

                  OK thats fine, but what happens when I **NEED** a CStringArray in my structure. (Right now i have replaced the CStringArray by a char**) but thats not very elegant!)

                  H Offline
                  H Offline
                  Holger Persch
                  wrote on last edited by
                  #9

                  The "=" operator can look like this:

                  class CMyClass
                  {
                  .
                  .
                  .

                  const CMyClass &operator =(const CMyClass &src)
                  {
                  	m\_String = src.m\_String;
                  
                  	m\_StringArray.RemoveAll();
                  	m\_StringArray.Append(src.m\_StringArray);
                  	m\_StringArray.FreeExtra();
                  
                  	return \*this;
                  }
                  

                  public:
                  CString m_String;
                  CStringArray m_StringArray;
                  };

                  Best regards Holger Persch

                  R T 2 Replies Last reply
                  0
                  • H Holger Persch

                    The "=" operator can look like this:

                    class CMyClass
                    {
                    .
                    .
                    .

                    const CMyClass &operator =(const CMyClass &src)
                    {
                    	m\_String = src.m\_String;
                    
                    	m\_StringArray.RemoveAll();
                    	m\_StringArray.Append(src.m\_StringArray);
                    	m\_StringArray.FreeExtra();
                    
                    	return \*this;
                    }
                    

                    public:
                    CString m_String;
                    CStringArray m_StringArray;
                    };

                    Best regards Holger Persch

                    R Offline
                    R Offline
                    Richard Lewis
                    wrote on last edited by
                    #10

                    Thanks so much for this Holger, that was very kind of you.

                    1 Reply Last reply
                    0
                    • R Richard Lewis

                      Do you mean implementing a = operator that copies from one CStringArray into another. P.S:I say this, because i am now using a statically allocated char[][], thereby eliminating the need for copying!

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #11

                      Yes - you need to impliment operator = for CStringArray, as Chris has already said twice now. As he has also said, the MFC container classes are plain ugly compared to STL, and std::vector would solve all of these problems as well as opening up a whole world of elegant container design to you. Christian Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

                      1 Reply Last reply
                      0
                      • H Holger Persch

                        The "=" operator can look like this:

                        class CMyClass
                        {
                        .
                        .
                        .

                        const CMyClass &operator =(const CMyClass &src)
                        {
                        	m\_String = src.m\_String;
                        
                        	m\_StringArray.RemoveAll();
                        	m\_StringArray.Append(src.m\_StringArray);
                        	m\_StringArray.FreeExtra();
                        
                        	return \*this;
                        }
                        

                        public:
                        CString m_String;
                        CStringArray m_StringArray;
                        };

                        Best regards Holger Persch

                        T Offline
                        T Offline
                        Tomasz Sowinski
                        wrote on last edited by
                        #12

                        Is there any reason for which you prefer RemoveAll/Append/FreeExtra over CStringArray::Copy? Tomasz Sowinski -- http://www.shooltz.com

                        *** Si fractum non sit, noli id reficere. ***

                        H 1 Reply Last reply
                        0
                        • T Tomasz Sowinski

                          Is there any reason for which you prefer RemoveAll/Append/FreeExtra over CStringArray::Copy? Tomasz Sowinski -- http://www.shooltz.com

                          *** Si fractum non sit, noli id reficere. ***

                          H Offline
                          H Offline
                          Holger Persch
                          wrote on last edited by
                          #13

                          No, i have just not recognized that there exists a copy method :-O. Best regards Holger Persch

                          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