Solved

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

Posted on 2004-04-06
9
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 6

Expert Comment

by:joghurt
ID: 10763983
> 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
ID: 10764023
'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
ID: 10764056
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:elbeardo69
ID: 10764108
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
 
LVL 16

Accepted Solution

by:
nonubik earned 120 total points
ID: 10764128
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
ID: 10764201
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
ID: 10764205
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
ID: 10764279
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
ID: 10764291
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

705 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