CArray + Structs Error...

Aug 31, 2011 at 9:38am
Hi all,

First at all thanks for the possible answers ;)
I tried to find a similar post... but i couldnt find it. Sry if there is one like this.

Well my problem is about CArray and Structs...
Lets see if someone could give me some guidelines :)

I declare a struct in the header:
1
2
3
4
5
struct M{
CString a;
CString b;
CArray <CString, CString> c;
};

Then in my class if i want to Add some objects...

1
2
3
4
CArray <M,M> d;
M auxM; // I insert all the values (I omit this step)

d.Add(auxM);

the compiler throws this error:
error C2664: 'Add' : cannot convert parameter 1 from 'struct M' to 'struct M'

Is it possible to do this?
May someone help me a little with this problem?

Thanks in advance.

Regards.
Last edited on Aug 31, 2011 at 10:05am
Aug 31, 2011 at 9:49am
I am bad in MFC, but I know, that it's better to use constant reference to the type as the second template argument in MFC collections, such as CArray. So, try this:
1
2
3
4
5
6
7
8
9
10
struct M{
CString a;
CString b;
CArray <CString> c; // second default argument is const T&
};

// ...

CArray <M> d; // second default argument is const T&
Aug 31, 2011 at 10:04am
Thanks for your fast reply :)

I changed the code to what you told me but... it gives me now these errors...

error C2976: 'CArray' : too few template arguments
d:\program files\microsoft visual studio\vc98\mfc\include\afxtempl.h(238) : see declaration of 'CArray'
d:\x.h(19) : error C2079: 'c' uses undefined class 'CArray<class CString>'
d:\x.h(25) : error C2976: 'CArray' : too few template arguments
d:\program files\microsoft visual studio\vc98\mfc\include\afxtempl.h(238) : see declaration of 'CArray'
d:\x.h(25) : error C2079: 'd' uses undefined class 'CArray<struct M>'

uhm any clue?
Last edited on Aug 31, 2011 at 10:04am
Aug 31, 2011 at 10:13am
I think i have the solution...

A few mins

//I thought so...
Last edited on Aug 31, 2011 at 10:40am
Aug 31, 2011 at 10:40am

1
2
3
4
5
6
7
8
9
10
11
12

struct Met{
	CString a;
	CString b;
	CArray<CString,CString&> c;
};

class M{
public:
	CArray<Met,Met&> d;
};


And in my "main" when i use:
Met auxM;

M x;
x.d.Add(auxM);

I used that and it appears...

d:\program files\microsoft visual studio\vc98\mfc\include\afxtempl.h(443) : error C2582: 'Met' : 'operator =' function is unavailable
d:\program files\microsoft visual studio\vc98\mfc\include\afxtempl.h(1566) : while compiling class-template member function 'void __thiscall CArray<struct Met,struct Met &>::SetAtGrow(int,struct Met&)'
Error executing cl.exe.

uhm any help about that?
Last edited on Aug 31, 2011 at 10:46am
Aug 31, 2011 at 11:17am
Seems to work like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Met{
	CString a;
	CString b;
	CArray<CString,CString&> c;
void operator=(const Met& m){}
};

class M{
public:
	CArray<Met,Met&> d;
};

If someone knows a better way of declaring CArrays with Structs pls let me know thanks
Sep 1, 2011 at 8:00am


Is not working properly, someone may be able to see the mistake?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Header:

struct Met{
	CString a;
	CString b;

	CArray <CString,CString&> c;// second default argument is const T&
	void operator=(const Met& m){}
};

class metS{
	public:

CArray <Met,Met&> d;


1
2
3
4
5
Class:
metS m_Met;
Met auxM;
... //auxM Filled; (Gets data properly)
m_Met.d.Add(auxM);


The problem im having at the moment... is that when using the function ".Add" from CArray. It increases the size of the CArray<Met,Met&> d. But inside there is nothing. It insert empty elements instead of the auxM(Which is filled with data).

Thanks for the possible answers.

Regards
Sep 1, 2011 at 8:28am
Solved and Works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Header:

struct Met{
	CString a;
	CString b;

	CArray <CString,CString&> c;
	void operator=(const Met& m){
        a = m.a;
        b = m.b;
        c.copy(m.c);
        }
};

1
2
3
4
5
Class:
metS m_Met;
Met auxM;
... //auxM Filled; (Gets data properly)
m_Met.d.Add(auxM);


Thanks

Regards
Topic archived. No new replies allowed.