CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    May 2006
    Posts
    172

    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
    Last edited by poshyradha; December 18th, 2007 at 12:51 PM. Reason: Just now learned to include code tag, so tried implementing

  2. #2
    Join Date
    May 2006
    Posts
    172

    Re: Problem with CArray inside a structure

    see declaration of 'CObject:perator ='

    It was originally like this --- [see declaration of 'CObject : : operator =']
    the Scope resolution operator was interrepted as smiley

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    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

  4. #4
    Join Date
    May 2006
    Posts
    172

    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

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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

  6. #6
    Join Date
    May 2006
    Posts
    172

    Re: Problem with CArray inside a structure

    Dear philip
    Thanks a lot for the explanation []
    Regards
    RK

  7. #7
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    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.
    If there is no love sun won't shine

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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 ?

  9. #9
    Join Date
    Jan 2005
    Posts
    111

    Re: Problem with CArray inside a structure

    As Philip said implement a default constructor, Copy Constructor and assignment operator in your structure.

  10. #10
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570
    Quote Originally Posted by Philip Nicoletti
    What would be the point of using CStringList ?
    Ah! sorry probably CStringArray then.

    He wants to store CString in a container isn't it?
    If there is no love sun won't shine

  11. #11
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    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.
    If there is no love sun won't shine

  12. #12
    Join Date
    May 2006
    Posts
    172

    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....

  13. #13
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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.

  14. #14
    Join Date
    May 2006
    Posts
    172

    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.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured