Solved

using dynamic CArray -- need an array of structures

Posted on 1998-04-28
4
286 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries DB2 Query 2 97
scoreUp challenge 14 64
sumHeights  challenge 17 74
Problem to App source 6 72
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

828 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