Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code:
C++
struct elemento
	{
		CString str_valore;
		CString str_nome;
	};

	CArray <elemento, elemento> m_arr;
	CArray <CArray <elemento, elemento>, CArray <elemento, elemento> > m_arr1;
but when I do:
C++
m_arr1.Add(m_arr);

I have an error..I can't put m_arr in m_arr1

What I have tried:

have an error and when I click on error vs shows this code:
 void CArray<type, arg_type="">::SetAtGrow(INT_PTR nIndex, ARG_TYPE newElement)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0);

if(nIndex < 0)
AfxThrowInvalidArgException();

if (nIndex >= m_nSize)
SetSize(nIndex+1, -1);
m_pData[nIndex] = newElement;
}
Posted
Updated 27-Jul-23 19:39pm
v4
Comments
Richard MacCutchan 27-Jul-23 4:24am    
"I have an error"
And of all the possible errors that exist in the universe which one is it?
Member 14594285 27-Jul-23 4:27am    
Impossible to do reference to function CArray.. I can't put m_arr in m_arr1
Rick York 27-Jul-23 11:24am    
As Mr. Pallini mentioned, std::vector would be a better option. I have used MFC for a long, long time and I still do but I never use the MFC collections or strings. I always use STL classes like vector and string. In my opinion, MFC is good for handling windows, dialogs, and controls but I do not like their collections at all.

You cannot do that. An explanation here: StackOverflow - How to create "CArray<carray<cstring>>& results" in MFC?[^] where you may find a workaround:
C++
CArray <elemento, elemento> m_arr;
CArray <CArray <elemento, elemento> * , CArray <elemento, elemento> * > m_arr1;
m_arr1.Add(&m_arr);
However that's ugly.
I would drop CArray in favour of std::vector
C++
vector < elemento > ve;
vector < vector < elemento > > vve;
vve.push_back(ve);
 
Share this answer
 
Comments
UTO 27-Jul-23 5:23am    
I agree.
If you still want to use CArray then the assignment operator must be implemented.
Member 14594285 27-Jul-23 5:37am    
thaks very much, it works
CPallini 27-Jul-23 5:50am    
You are welcome.
Rick York 27-Jul-23 16:18pm    
I agree. I try to avoid all MFC collections and CString. I can't always but in the vast majority of cases I can.
At a guess since m_arr1 is an array of items that each comprise a pair of CArray <elemento, elemento> types, you are missing a value when you try to add a single CArray <elemento, elemento>.
 
Share this answer
 
Comments
Member 14594285 27-Jul-23 4:35am    
In this way? :

struct elemento
{
CString str_valore;
CString str_nome;
};

CArray <elemento, elemento=""> m_arr;
CArray <carray <elemento,="" elemento="">> m_arr1;

but I have an error:

attempt to reference a deleted function
Richard MacCutchan 27-Jul-23 4:42am    
Please use the Improve question link above, and add complete details of what is not working.
The second template argument of the CArray class specifies the argument type that is used to access objects stored in the array. Being the objects CArray they can be accessed only by reference.
Try this:

CArray <CArray <elemento, elemento>, CArray <elemento, elemento>&> m_arr1;
 
Share this answer
 
v2
Comments
Member 14594285 27-Jul-23 4:47am    
I have an error and when I click on error vs shows this code:

void CArray<type, arg_type="">::SetAtGrow(INT_PTR nIndex, ARG_TYPE newElement)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0);

if(nIndex < 0)
AfxThrowInvalidArgException();

if (nIndex >= m_nSize)
SetSize(nIndex+1, -1);
m_pData[nIndex] = newElement;
}
UTO 27-Jul-23 5:11am    
The assignment operator is missing.
The CArray class needs copyable types.
Here the amended code. The "copy" code is missing (see the comments)
          class CMyArray : public CArray<elemento, elemento>
          {
          public:
            CMyArray& operator=(const CMyArray& ma) {
              /* TODO: code to copy ma into *this */
              return *this;
            };
          } m_arr;
          CArray<CMyArray,CMyArray&> m_arr1;
          m_arr1.Add(m_arr);
Member 14594285 27-Jul-23 5:14am    
so must I do a class? I can't modifie my code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900