We help IT Professionals succeed at work.

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

elbeardo69
elbeardo69 asked
on
Medium Priority
534 Views
Last Modified: 2013-11-20
Hi,

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

CArray<CString,CString>m_FilterSelArray;
CArray<CString,CString>m_VFilterArray;
CArray<CString,CString>m_AFilterArray;

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

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

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
Comment
Watch Question

Commented:
> 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
Parameters
TYPE
Template parameter specifying the type of objects stored in the array. TYPE is a parameter that is returned by CArray.
ARG_TYPE
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.

Commented:
'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 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_MFC_CArray.asp)

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?

Author

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.

Author

Commented:
If I try cutting

CArray<CString,CString>m_VFilterArray;

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:
m_VFilterArray

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..
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
nonubik, thanks, that helped, but did not solve the problem. Does declaring

CArray<CString,CString>m_AFilterArray;

in a header file work for anybody??
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

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

Author

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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.