CArrays... by 'The Beginner'...


I think i've struggled as far as to realise one cannot declare a Template object (which CArray is?!) in the header file.. but why not?

I've declared them under the includes for the .cpp file to get public access, is this correct, or should it be in initDialoag?... I just don't know. I include,

#include "afxtempl.h"   ... and declare three CArrays..


.. could someone fill me in as to what 'CArray<CString,CString&>' means.. same as 'CArray<CString,&CString>'?

void CABCFilmDlg::OnOpenFiltersButton()
  while(count < str.GetLength())
    else if(ch == '\n')
        case 1:
          filmPart = "";
        case 2:                              
//       m_AFilterArray.Add(filmPart);
          filmPart = "";
        case 3:                              
//       m_CFilterArray.Add(filmPart);
          filmPart = "";

This builds fine.. and works.. however if either of the comments '//' are removed so I can use the other CArrays, I get the compile error,

...\ABCFilmDlg.cpp(369) : error C2228: left of '.Add' must have class/struct/union type.

It just seems like one is only allowed to use 1 CArray object which is a bonkers concept. What am I doing wrong?

Also.. is it necessary to cast when retrieving a CArray member?

I know that there are a million ways to store an array of strings.. please don't give me alternatives, I'm trying to learn the language, not how to ignore the bits I get stuck with.

Thank you
Who is Participating?
nonubikConnect With a Mentor Commented:
You have to inlcude <afxtempl.h> in your header or in your cpp before the inclusion of your header like:

#include <afxtempl.h>
#include "abcfilmdlg.h"

Or better, inlcude it in your stdafx.h, if you use precompiled headers
> could someone fill me in as to what 'CArray<CString,CString&>' means.. same as 'CArray<CString,&CString>'?

CString& is a reference to a CString object (right now think of it as if it were CString*).
&CString makes less sence because & before anything means the address-of operator. (And of course the declaration of CArray uses the first form.)

Copied from MSDN:
"template < class TYPE, class ARG_TYPE = const TYPE& >
class CArray :
   public CObject
Template parameter specifying the type of objects stored in the array. TYPE is a parameter that is returned by CArray.
Template parameter specifying the argument type used to access objects stored in the array. Often a reference to TYPE. ARG_TYPE is a parameter that is passed to CArray. "

And the names of the three arrays you've declared differs from the three array names you use in your method (m_FilterSelArray vs m_CFilterArray). Otherwise it compiles for me if I remove the comments from case 2.
'CArray<CString,&CString>' is illegal (will not compile)
'CArray<CString,CString&>' means that the template parameter specifying the argument type used to access objects stored in the array is a reference to CString (for more info see

I can hardly believe that one cannot declare a template object in a header file (please post your code that made you jump to this conclusion).

Then I see no declaration for m_CFilterArray. And maybe your compile error is form something else. Could you pleas post more of source code?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

elbeardo69Author Commented:
Sorry.. should have checked the post more thoroughly. I actually have six CArrays delcared in the class, I simply cut and pasted the wrong selection. There are no typos in my real code. There are no 'undeclared identifier' errors on compile! .. but thank you for the speedy reply.
elbeardo69Author Commented:
If I try cutting


from abcfilmdlg.cpp and pasting to its header... I get..

d:\apps\abcfilm\abcfilmdlg.h(33) : error C2143: syntax error : missing ';' before '<'
d:\apps\abcfilm\abcfilmdlg.h(33) : error C2501: 'CArray' : missing storage-class or type specifiers
d:\apps\abcfilm\abcfilmdlg.h(33) : error C2059: syntax error : '<'
d:\apps\abcfilm\abcfilmdlg.h(33) : error C2238: unexpected token(s) preceding ';'
D:\apps\ABCFilm\ABCFilmDlg.cpp(362) : error C2065: 'm_VFilterArray' : undeclared identifier
D:\apps\ABCFilm\ABCFilmDlg.cpp(362) : error C2228: left of '.Add' must have class/struct/union type

or.. in the WorkSpace ClassView, if I try to 'Add Memeber Variable' to the class..

Variable Type:
CArray<CString, CString>

Variable Name:

I get an error alert...

'Template declarations or definitions cannot be added'.

.. I would post the whole code.. but it's in a horribly messy embarrassing state..
elbeardo69Author Commented:
nonubik, thanks, that helped, but did not solve the problem. Does declaring


in a header file work for anybody??
joghurtConnect With a Mentor Commented:
Yes, if you move your array declaration to a header then you must #include <afxtempl.h> in your header file (or better in stdafx.h).
Of course it works. But if you declare the variable global and not as a class member you might get linker errors if you include your header in more than one cpp.
elbeardo69Author Commented:
Always turns out to be so basic hey... thank you both...

to note..

#include <afxtempl.h>
#include "abcfilmdlg.h"

in the abcfilmdlg.cpp source file doesn't do the same as

#include <afxtempl.h>

actually in the header file.... hum.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.