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 woes

CArray woes

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structureshelplearning
19 Posts 5 Posters 1 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 Christian Graus

    Can I also recommend that now is a good time to learn about vector, which is part of the STL ( i.e. part of C++ instead of part of Microsofts proprietary code ), and is far better than CArray ? CArray works fine, and the advice you've been given should get it working, but you will eventually wish it did something that the STL does, so now is a good time to learn the better alternative. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
    C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
    Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #5

    I'd also like to learn STL with all its useful classes, but I don't know where to start, since STL is quite complex and has many functions like vector, list etc. Can you suggest me some good tutorials on STL apart from the ones here at cp.com? thanks in advance Greg

    C 1 Reply Last reply
    0
    • L Lost User

      I'd also like to learn STL with all its useful classes, but I don't know where to start, since STL is quite complex and has many functions like vector, list etc. Can you suggest me some good tutorials on STL apart from the ones here at cp.com? thanks in advance Greg

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

      The ones here are easier ( I hope so, I wrote some of them ), but the definitive reference is www.sgi.com/tech/stl ( from memory, if it's wrong go to www.sgi.com and look for it ). That's where I go. Addison Wesley have several good books on STL, and I know that the main one on the standard library is available through Safari ( safari.oreilly.com ). Have you looked at my tutorials here ? Is there any way in which they are not clear ? Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
      C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
      Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

      L 1 Reply Last reply
      0
      • C Christian Graus

        The ones here are easier ( I hope so, I wrote some of them ), but the definitive reference is www.sgi.com/tech/stl ( from memory, if it's wrong go to www.sgi.com and look for it ). That's where I go. Addison Wesley have several good books on STL, and I know that the main one on the standard library is available through Safari ( safari.oreilly.com ). Have you looked at my tutorials here ? Is there any way in which they are not clear ? Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
        C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
        Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #7

        I have read all your tutorials about STL, and they are all great :) But I think that they cover only the basics (but they to that very well), and I'd like to go more into detail and get to know all the facets of STL. Thanks for the SGI link, seems to be exactly what I was looking for :) best regards Greg

        C 1 Reply Last reply
        0
        • R Roman Fadeyev

          Firstly, i advise you to read at least a couple pages of any book about templates. It will help you to control a situation. As for your question, you should type: CArray < SRawData, SRawData& > m_array1; Notice that your function

          CArray CRainAttDoc::GetRawDataArray()

          will copy whole array since you return it by value, not by ref. It is better way to use

          const CArray<......> & CRainAttDoc::GetRawDataArray() const

          R Offline
          R Offline
          raner
          wrote on last edited by
          #8

          Thanks for your advice but i don't really understand the following const CArray<......> & CRainAttDoc::GetRawDataArray() const Can you teach me more explicitly how to pass by reference?I've tried passing a reference to an array before but it didn't work out. Once again, thank you.

          1 Reply Last reply
          0
          • K KaRl

            Try this: in your header file, declare outside of the doc class declaration typedef CArray CSRawDataArray; In the document class definition: CSRawDataArray m_myDataArray; Now you may use m_myDataArray like SRawData *pNewData = new SRawData; m_myDataArray.Add(pNewData); while(m_myDataArray.GetCount()) delete m_myDataArray.RemoveHead() HTH, K.


            One small village of indomitable geeks still holds out against the invaders. And life is not easy for the managers legionaries who garrison the fortified camps of Microsoftum, Javum, Ceplumplum and Vebasum

            R Offline
            R Offline
            raner
            wrote on last edited by
            #9

            haa ok i said thks for your suggestion.gave me quite some insight really.But i still dunno how to create a function to return the array in some way so that other classes may access it.u have any suggestion on that?

            K 1 Reply Last reply
            0
            • R raner

              haa ok i said thks for your suggestion.gave me quite some insight really.But i still dunno how to create a function to return the array in some way so that other classes may access it.u have any suggestion on that?

              K Offline
              K Offline
              KaRl
              wrote on last edited by
              #10

              There are several solutions: * You may add public member functions to your document to manipulate the array from the outside of the document object * You may directly "lend" the array to other classes like this: CSRawDataArray &CMyDocument::GetArray() { return m_myDataArray; } Beware that this method returns a reference on the array, i.e. the "real" object, not a copy. void COtherClass::AddData(CMyDocument *pDoc, CSRawDataArray *pData) { CSRawDataArray &myArray = pDoc->GetArray(); myArray.Add(pData); } HTH, K.


              One small village of indomitable geeks still holds out against the invaders. And life is not easy for the managers legionaries who garrison the fortified camps of Microsoftum, Javum, Ceplumplum and Vebasum

              R 1 Reply Last reply
              0
              • L Lost User

                I have read all your tutorials about STL, and they are all great :) But I think that they cover only the basics (but they to that very well), and I'd like to go more into detail and get to know all the facets of STL. Thanks for the SGI link, seems to be exactly what I was looking for :) best regards Greg

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

                I see - I thought you were saying the opposite, that you needed something easier to understand. Yes, the SGI site is an excellent place to take it further. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
                C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
                Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

                1 Reply Last reply
                0
                • K KaRl

                  There are several solutions: * You may add public member functions to your document to manipulate the array from the outside of the document object * You may directly "lend" the array to other classes like this: CSRawDataArray &CMyDocument::GetArray() { return m_myDataArray; } Beware that this method returns a reference on the array, i.e. the "real" object, not a copy. void COtherClass::AddData(CMyDocument *pDoc, CSRawDataArray *pData) { CSRawDataArray &myArray = pDoc->GetArray(); myArray.Add(pData); } HTH, K.


                  One small village of indomitable geeks still holds out against the invaders. And life is not easy for the managers legionaries who garrison the fortified camps of Microsoftum, Javum, Ceplumplum and Vebasum

                  R Offline
                  R Offline
                  raner
                  wrote on last edited by
                  #12

                  Hi..i've tried the following but it's still not working.Do you know wat i shld do? ------------------------------------------------------------------- typedef CArray CSRawDataArray;//outside class declaration //in doc header CSRawDataArray m_array1; CSRawDataArray& GetRawDataArray() const; //in doc source file //doing 2 things here.(1)load data into array from text file (2)define get function void CRainAttDoc::Serialize(CArchive& ar) { CString strOneLine; while(ar.ReadString(strOneLine)) { SRawData *pData = new SRawData; sscanf(strOneLine,"%g%g\n",pData->x,pData->y); m_array1.Add(pData); } } CSRawDataArray &CRainAttDoc::GetRawDataArray() const { return m_array1; } --------------------------------------------------------------------- This gives me the following error: error C2440: 'return' : cannot convert from 'const class CArray' to 'class CArray &' When i try to change the object declaration to this: const CSRawDataArray m_array1; error C2440 remains and on top of that, the following message for RemoveAll() and Add() 'RemoveAll' : cannot convert 'this' pointer from 'const class CArray' to 'class CArray &' I was trying to declare the function const so that other classes cant alter the value as suggested by Roman Fadeyev but it just cant work.I'm pulling my hair out!..haa thks alot for your help.

                  K 1 Reply Last reply
                  0
                  • R raner

                    Hi..i've tried the following but it's still not working.Do you know wat i shld do? ------------------------------------------------------------------- typedef CArray CSRawDataArray;//outside class declaration //in doc header CSRawDataArray m_array1; CSRawDataArray& GetRawDataArray() const; //in doc source file //doing 2 things here.(1)load data into array from text file (2)define get function void CRainAttDoc::Serialize(CArchive& ar) { CString strOneLine; while(ar.ReadString(strOneLine)) { SRawData *pData = new SRawData; sscanf(strOneLine,"%g%g\n",pData->x,pData->y); m_array1.Add(pData); } } CSRawDataArray &CRainAttDoc::GetRawDataArray() const { return m_array1; } --------------------------------------------------------------------- This gives me the following error: error C2440: 'return' : cannot convert from 'const class CArray' to 'class CArray &' When i try to change the object declaration to this: const CSRawDataArray m_array1; error C2440 remains and on top of that, the following message for RemoveAll() and Add() 'RemoveAll' : cannot convert 'this' pointer from 'const class CArray' to 'class CArray &' I was trying to declare the function const so that other classes cant alter the value as suggested by Roman Fadeyev but it just cant work.I'm pulling my hair out!..haa thks alot for your help.

                    K Offline
                    K Offline
                    KaRl
                    wrote on last edited by
                    #13

                    I don't understand why you're using the keyword "const". Have you tried without it ? If you want to be sure the others classes don't mess up with the array, you may declare it as protected in your document class and add public methods (to your document class), to be sure it's the only class which will manipulate directly the array.


                    One small village of indomitable geeks still holds out against the invaders. And life is not easy for the managers legionaries who garrison the fortified camps of Microsoftum, Javum, Ceplumplum and Vebasum

                    R 1 Reply Last reply
                    0
                    • K KaRl

                      I don't understand why you're using the keyword "const". Have you tried without it ? If you want to be sure the others classes don't mess up with the array, you may declare it as protected in your document class and add public methods (to your document class), to be sure it's the only class which will manipulate directly the array.


                      One small village of indomitable geeks still holds out against the invaders. And life is not easy for the managers legionaries who garrison the fortified camps of Microsoftum, Javum, Ceplumplum and Vebasum

                      R Offline
                      R Offline
                      raner
                      wrote on last edited by
                      #14

                      oh yah you are right, i totally forgot i can declare it as protected. yes, it can be compiled now.however when trying to open the text file after executing the program, the serialize() function fails and returns a what appear to be an assertion failure.

                      K 1 Reply Last reply
                      0
                      • R raner

                        oh yah you are right, i totally forgot i can declare it as protected. yes, it can be compiled now.however when trying to open the text file after executing the program, the serialize() function fails and returns a what appear to be an assertion failure.

                        K Offline
                        K Offline
                        KaRl
                        wrote on last edited by
                        #15

                        I suppose your Serialize method is like void CRainAttDoc::Serialize(CArchive& ar) { CString strOneLine; while(ar.ReadString(strOneLine)) { SRawData *pData = new SRawData; sscanf(strOneLine,"%g%g\n",pData->x,pData->y); m_array1.Add(pData); } } First of all, you should test if you are writing or reading, using ar.IsStoring() (Serialization works both way). Also instead using a "while" statement, I would suggest you to write explicitly at the beginning of the archive the number of items to read It would look like this void CRainAttDoc::Serialize(CArchive& ar) { if(ar.IsStoring()){ // writing the data int iSize = m_array1.GetSize(); ar << iSize; for(int i = 0; i w iSize; i++){ // write each element here in the archive ... } }else{ // reading the data m_array1.RemoveAll(); int iSize; ar >> iSize; m_Array1.SetSize(iSize); for(int i = 0; i < iSize; i++){ // read one element here and add it to the array ... } } } I'm not absolutly sure of the syntax, I write it from memory :) HTH, K.


                        One small village of indomitable geeks still holds out against the invaders. And life is not easy for the managers legionaries who garrison the fortified camps of Microsoftum, Javum, Ceplumplum and Vebasum

                        R 1 Reply Last reply
                        0
                        • K KaRl

                          I suppose your Serialize method is like void CRainAttDoc::Serialize(CArchive& ar) { CString strOneLine; while(ar.ReadString(strOneLine)) { SRawData *pData = new SRawData; sscanf(strOneLine,"%g%g\n",pData->x,pData->y); m_array1.Add(pData); } } First of all, you should test if you are writing or reading, using ar.IsStoring() (Serialization works both way). Also instead using a "while" statement, I would suggest you to write explicitly at the beginning of the archive the number of items to read It would look like this void CRainAttDoc::Serialize(CArchive& ar) { if(ar.IsStoring()){ // writing the data int iSize = m_array1.GetSize(); ar << iSize; for(int i = 0; i w iSize; i++){ // write each element here in the archive ... } }else{ // reading the data m_array1.RemoveAll(); int iSize; ar >> iSize; m_Array1.SetSize(iSize); for(int i = 0; i < iSize; i++){ // read one element here and add it to the array ... } } } I'm not absolutly sure of the syntax, I write it from memory :) HTH, K.


                          One small village of indomitable geeks still holds out against the invaders. And life is not easy for the managers legionaries who garrison the fortified camps of Microsoftum, Javum, Ceplumplum and Vebasum

                          R Offline
                          R Offline
                          raner
                          wrote on last edited by
                          #16

                          Thks alot. To read each element and add it to the array, can i use the code below? SRawData *pData = new SRawData; sscanf(strOneLine,"%g%g\n",pData->x,pData->y); m_array1.Add(pData);

                          K 1 Reply Last reply
                          0
                          • R raner

                            Thks alot. To read each element and add it to the array, can i use the code below? SRawData *pData = new SRawData; sscanf(strOneLine,"%g%g\n",pData->x,pData->y); m_array1.Add(pData);

                            K Offline
                            K Offline
                            KaRl
                            wrote on last edited by
                            #17

                            Why not using directly the archive ? When loading: SRawData *pData = new SRawData; ar >> pData->x; ar >> pData->y; m_array1.Add(pData); When writing: SRawData *pData = m_array1.GetAt(i); ar << pData->x; ar << pData->y; HTH, K.


                            One small village of indomitable geeks still holds out against the invaders. And life is not easy for the managers legionaries who garrison the fortified camps of Microsoftum, Javum, Ceplumplum and Vebasum

                            R 1 Reply Last reply
                            0
                            • K KaRl

                              Why not using directly the archive ? When loading: SRawData *pData = new SRawData; ar >> pData->x; ar >> pData->y; m_array1.Add(pData); When writing: SRawData *pData = m_array1.GetAt(i); ar << pData->x; ar << pData->y; HTH, K.


                              One small village of indomitable geeks still holds out against the invaders. And life is not easy for the managers legionaries who garrison the fortified camps of Microsoftum, Javum, Ceplumplum and Vebasum

                              R Offline
                              R Offline
                              raner
                              wrote on last edited by
                              #18

                              Because i'm not familiar with CArchive and have never tried using them before ;P ..Anyway i've tried the following.I think this has the right syntax,i've checked my books.However, it returns a "Failed to open document" dialog so i wrote the try-catch exception handling. And it turns out that SetSize()is indeed creating problem. And the "Testing" Message Box appears a number of times before "Unexpected file format" is displayed.The document i was opening is a text file with 14 rows, 2 columns of values. if(ar.IsStoring()) { // writing the data int iSize = m_array1.GetSize(); ar << iSize; for(int i = 0; i < iSize; i++) { SRawData *pData = m_array1.GetAt(i); ar << pData->x; ar << pData->y; } } else { m_array1.RemoveAll(); int iSize=13; ar >> iSize; try{ m_array1.SetSize(iSize); } catch(CMemoryException *e) { AfxMessageBox("Cant set size"); e->Delete(); } for (int i = 0; i < iSize; i++) { AfxMessageBox("Testing"); SRawData *pData = new SRawData; ar >> pData->x; ar >> pData->y; m_array1.Add(pData); } } any idea wat is wrong now?Really appreciate your patience;)

                              K 1 Reply Last reply
                              0
                              • R raner

                                Because i'm not familiar with CArchive and have never tried using them before ;P ..Anyway i've tried the following.I think this has the right syntax,i've checked my books.However, it returns a "Failed to open document" dialog so i wrote the try-catch exception handling. And it turns out that SetSize()is indeed creating problem. And the "Testing" Message Box appears a number of times before "Unexpected file format" is displayed.The document i was opening is a text file with 14 rows, 2 columns of values. if(ar.IsStoring()) { // writing the data int iSize = m_array1.GetSize(); ar << iSize; for(int i = 0; i < iSize; i++) { SRawData *pData = m_array1.GetAt(i); ar << pData->x; ar << pData->y; } } else { m_array1.RemoveAll(); int iSize=13; ar >> iSize; try{ m_array1.SetSize(iSize); } catch(CMemoryException *e) { AfxMessageBox("Cant set size"); e->Delete(); } for (int i = 0; i < iSize; i++) { AfxMessageBox("Testing"); SRawData *pData = new SRawData; ar >> pData->x; ar >> pData->y; m_array1.Add(pData); } } any idea wat is wrong now?Really appreciate your patience;)

                                K Offline
                                K Offline
                                KaRl
                                wrote on last edited by
                                #19

                                Check your mail :)


                                One small village of indomitable geeks still holds out against the invaders. And life is not easy for the managers legionaries who garrison the fortified camps of Microsoftum, Javum, Ceplumplum and Vebasum

                                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