Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

HELP!!!: Passing collection objects???

Posted on 1998-10-24
2
Medium Priority
?
239 Views
Last Modified: 2013-11-20
How to pass collection classes as argument for template & non-template type???

I've tried both but failed..... quite frustrating...

In my code,

CStringArray aString;
CArray <CString, LPCSTR> str;
CMyClass obj;

obj.method(aString, str);

=======
void CMyClass::
      method(CStringArray aStr, CArray<CString, LPCSTR> str)
{
   ......
}

Then I got this error

C2664: cannot convert parameter 1 from 'class CStringArray' to 'class CStringArray'

======
Pls show me the right way.... Thanks......
0
Comment
Question by:samyee
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
ID: 1323788
CStringArray & CArray can not be copied.  The "method" member functon when called attempts to generate a copy into the local variables used by the procedure.

(1) Use a reference or pointer to the type in the function declaration, to avoid copying, OR (2) derive a class from CArray/CStringArray which includes copying functionality.

A reference would look like this:

     void CMyClass::
           method(CStringArray& aStr, CArray<CString, LPCSTR>& str)
     {
        ......
     }


note the ampersands

Remember method is working with with the original variable now (under a "pseudo name").  If "method" is not supposed to be able to change the array variables, add const to one or both array parameters :

     void CMyClass::
           method( const CStringArray& aStr, const CArray<CString, LPCSTR>& str)
     {
        ......
     }



0
 

Author Comment

by:samyee
ID: 1323789
Thanks... it's work :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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: 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…
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.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

877 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