Problem with CArray inside a structure
Hi all,
I have a Structure defined as follows
Code:
struct StationGroup
{
CString cStrGroupName;
CArray<CString,CString&> cArryStationList;
};
Which consist of a CArray of CString, Now i need to make a CArray of the structur, which i did as follows
Code:
CArray<StationGroup,StationGroup&> m_cArryGroupList;
when i am adding a single object of the structure "StationGroup" to the CArray "m_cArryGroupList" as follows
Code:
m_cArryGroupList.Add((StationGroup&)sTructStationGroup);
I get an error as follows and it no way indicates that the error is in the above line, but if i comment this line, Then i dont get the following error
Code:
PIConfigComm.cpp
c:\program files\microsoft visual studio 8\vc\atlmfc\include\afxtempl.h(272) : error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject'
c:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(554) : see declaration of 'CObject::operator ='
c:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(524) : see declaration of 'CObject'
This diagnostic occurred in the compiler generated function 'CArray<TYPE,ARG_TYPE> &CArray<TYPE,ARG_TYPE>::operator =(const CArray<TYPE,ARG_TYPE> &)'
with
[
TYPE=CString,
ARG_TYPE=CString &
]
where CPIConfigComm is the name of the class
Can some one please help me....
Thanks and Regards
RK
Re: Problem with CArray inside a structure
see declaration of 'CObject::operator ='
It was originally like this --- [see declaration of 'CObject : : operator =']
the Scope resolution operator was interrepted as smiley
Re: Problem with CArray inside a structure
You can use std::vector<CString> instead of CArray and avoid these problems.
Code:
#include <vector>
struct StationGroup
{
CString cStrGroupName;
std::vector<CString> cArryStationList; // now structure can be copied
};
Regards,
Paul McKenzie
Re: Problem with CArray inside a structure
Thanks Paul,
Just now trying to implement.
as I dont know the addition and delition and manupulating operation , I will let you know once I get trough
By the way can you tell me what the problem with the previous implementation? Just a curiosity to learn :-)
Thanks and Regards
RK
Re: Problem with CArray inside a structure
CArray does not have copy semantics (it has a private copy
constructor and assignment operator). To put objects of
type StationGroup into a CArray, StationGroup must be copyable.
Which means every member of StationGroup must be copyable.
You can do one of the following:
1) use std::vector as Paul mentioned (that is what I would do)
2) Add copy constructor and assignment operator to StationGroup
3) derive from CArray and add copy semantics to it
see http://www.codeguru.com/forum/showthread.php?t=351440
Re: Problem with CArray inside a structure
Dear philip
Thanks a lot for the explanation [:)]
Regards
RK
Re: Problem with CArray inside a structure
Why are using CArray<CString,CString&> cArryStationList;
You can use std::vector as others suggested. Or alternatively you can use CStringList. Look MSDN documentation for the usage of CStringList MFC class.
Re: Problem with CArray inside a structure
Quote:
Originally Posted by miteshpandey
Or alternatively you can use CStringList. Look MSDN documentation for the usage of CStringList MFC class.
What would be the point of using CStringList ?
Re: Problem with CArray inside a structure
As Philip said implement a default constructor, Copy Constructor and assignment operator in your structure.
Re: Problem with CArray inside a structure
Oops, sorry..........:)
My mind was more concentrated what was inside the struct rather than the struct itself.
Thanks for reminding.
Re: Problem with CArray inside a structure
Let me brief the purpose of my usage.
I have a list of Group names each group has a group name and a list of station
[i am not explaining what is group and station as it is purely business terms not a technical name]
so i made a structure with CString and a cstring array where the CString will contain the the group name and the cstring array will contain the list of stations.
after wich i will againg have a carry of the structure and store all the list of group into that array....
Re: Problem with CArray inside a structure
There are a number of designs that you can use. How you use the
data would determine which one is best.
In general, the design that you have chosen is fine. You just need
to make sure that StationGroup objects are copyable.
Other designs:
1) a map conatiner, with the key being the group name and the
value being the cArryStationList
2) a multimap container (which allows multiple values for each key).
Again, the group name would be the key and the values would be
the Station list names associated with that name.
Re: Problem with CArray inside a structure
Hi Philip,
Thanks for the suggestion,
Let me try that, as i mentioned earlier being new to Visual C++ i learning now. Will try this method and let me come back if at all i have any other complexcities in doing so.
Re: Problem with CArray inside a structure
Quote:
Originally Posted by poshyradha
Hi Philip,
Thanks for the suggestion,
Let me try that, as i mentioned earlier being new to Visual C++
Note that Visual C++ is just a compiler brand from Microsoft, it is not the language. The language is called just C++, and what Philip mentioned to you (as well as me mentioning std::vector) are general C++ classes and solutions that can be used with any ANSI compliant C++ compiler, not just Visual C++.
So what you're learning right now with respect to this thread is C++, and is not specific to the Visual C++ compiler.
Regards,
Paul McKenzie
Re: Problem with CArray inside a structure
Hi Paul,
As for many, This particular community (CodeGuru) has taught me a lot. Thanks once again.
as you mentioned can I do all these operation, whatever i do using Visual C++ editor, Can I do the same using any older version of compiler like Borland C++ or Turbo C++?
I remember those were the compliers i used during my school days :-)
Re: Problem with CArray inside a structure
Quote:
Originally Posted by poshyradha
as you mentioned can I do all these operation, whatever i do using Visual C++ editor, Can I do the same using any older version of compiler like Borland C++ or Turbo C++?
Not the older versions of the compiler, as far as I know. The C++ language was standardized in 1998, and these compilers are older than 1998.
You would need to use the latest "CodeGear" version of C++ Builder (which used to be the Borland compiler), which is ANSI compliant.
Quote:
I remember those were the compliers i used during my school days :-)
In this day and age, those old compilers shouldn't be used for newcomers learning the language because of the basic differences between writing programs the pre-ANSI standard way, and writing programs using the true ANSI standard C++ way.
For example:
Code:
#include <iostream.h>
int main()
{
cout << "Hello World";
}
This no longer compiles on an ANSI C++ compiler. It won't even compile using the latest Visual C++ compilers. Here is the correction:
Code:
#include <iostream>
int main()
{
std::cout << "Hello World";
}
Unfortunately, it seems that many new people are forced to use the old technology by their school or professors, and end up writing code that no longer compiles properly with modern compilers (which most companies use). So these students wind up wasting time learning C++ that, for the most part, doesn't exist for modern compilers.
Regards,
Paul McKenzie
Re: Problem with CArray inside a structure
Dear Paul,
U are well said........
In many industry the acedemics are entirely different from day-to-day industry practice
any how once again thanks a lot for your valuable time and patience you spent in giving me a clear cut explanation with examples
You people are doing a great job, It is of great help for freshers like me...
Thanks you once again
Regards
RK