CArray woes
-
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-2002I'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 -
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 GregThe 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 -
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-2002I 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
-
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
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.
-
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
-
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?
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
-
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
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 -
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
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.
-
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.
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
-
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
-
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.
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 thisvoid 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
-
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 thisvoid 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
-
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);
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
-
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
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;)
-
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;)