Solved

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

Posted on 2004-04-06
9
501 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
0
Comment
Question by:elbeardo69
  • 4
  • 3
  • 2
9 Comments
 
LVL 6

Expert Comment

by:joghurt
Comment Utility
> 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.
0
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
'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?
0
 
LVL 1

Author Comment

by:elbeardo69
Comment Utility
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.
0
 
LVL 1

Author Comment

by:elbeardo69
Comment Utility
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..
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Accepted Solution

by:
nonubik earned 120 total points
Comment Utility
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
0
 
LVL 1

Author Comment

by:elbeardo69
Comment Utility
nonubik, thanks, that helped, but did not solve the problem. Does declaring

CArray<CString,CString>m_AFilterArray;

in a header file work for anybody??
0
 
LVL 6

Assisted Solution

by:joghurt
joghurt earned 30 total points
Comment Utility
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).
0
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
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.
0
 
LVL 1

Author Comment

by:elbeardo69
Comment Utility
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.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now