Solved

using dynamic CArray -- need an array of structures

Posted on 1998-04-28
4
278 Views
Last Modified: 2013-11-19
I am designing a CVideoList class which will hold the an
array of this structure:

struct Video
{
   CString title;
   int have;  int want;
   int inventory_number;
   int noWaiting;
     <<a dynamic array which will hold :
   struct waitListEntry
   {
      CString waitName;
      CString contact;
   } waitList;
     >>>>>

 }

my trouble is :

1) when I compile, I find that CString is undefined
2) I understand how to use the templated class CArray

please explain:
1) which files to #include in my class header file
2) how to use dynamic CArray template class

Thank you in advance for your help.

0
Comment
Question by:ehb
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
dkremer earned 100 total points
ID: 1304229
Hi.
First thing, it'll better if you derived your classes from CObject - MFC's base class.
To answer your question:
1. Include the following in StdAfx.h:
#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxtempl.h>            // MFC Templates
2. Lets say your array variable is called m_WaitingList, and you want to set the fifth elemet, you call SetAtGrow(5, myWaitingListEntry) and if the array's upper bound is less than 5 CArray will dynamically change the size of the array for ya... :)
I think it's better to use the templatized list instead of array - it's more suitable for this case.
Check VC5 help URL : mk:@ivt:vccore/F26/D2B/S23C9.HTM
you can choose 'sample' there to see the COLLECT sample which uses all kind of data structures.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1304230
To use a dynamic array...

CArray<data-type, arg-type>

eg.

CArray<waitListEntry,const waitListEntry &>

this means each item in the array is a 'waitListEntry' and the member function args are 'const waitListEntry&'.  For example, the template will expand to include a member function

void SetAtGrow( int nIndex, const waitListEntry& newElement );

note that 'const waitListEntry&' was used to pass an element in/out.


See the on-line help for examples (as per the answer proposed)
0
 

Author Comment

by:ehb
ID: 1304231
Could you please expound upon deriving from CObject? Can I do this for any class I want?

I took a college level OODesign/Implementation class and deriving from CObject was never mentioned.

Thank you very much for your help!

Eric

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1304232
There is no need at all to derive from CObject - don't do it unless you really need the serialization.

You use the CArray<> (or CList<> or CMap<>) template classes.

These work equally well with non-CObject classes.

See my earlier comment .. Or better (for me) ask another question so I can ansewr it :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

25 Experts available now in Live!

Get 1:1 Help Now