?
Solved

Multidimensional CArrays?

Posted on 2004-04-20
7
Medium Priority
?
869 Views
Last Modified: 2013-11-20
Hi,
How can one represent multidimensional arrays using CArray objects?
I tried this with both CList and CArray:

typedef CList<int> CIntList; //Okay!
typedef CArray<CIntList> CArrayOfIntList; //Okay!

CArrayOfIntList x;

x.Add(CList()); //Error!

CIntList y;
x.Add(y); //Error!

What should the syntax be? is it even possible??

Thanks
0
Comment
Question by:gamesdev
[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
7 Comments
 
LVL 7

Expert Comment

by:leflon
ID: 10867840
Hi gamesdev,
i may be wrong but the definition of CArrayOfIntList should be

  typedef CArray<CIntList,CIntList> CArrayOfIntList;
or
  typedef CArray<CIntList, CIntList&> CArrayOfIntList;


leflon
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10867851
Try this...

typedef CArray<int, int> rgInt;
CArray<rgInt, rgInt> rgTest;
0
 
LVL 3

Accepted Solution

by:
akalmani earned 400 total points
ID: 10867891
I missed the usage of it....

typedef CArray<int, int> rgInt;
CArray<rgInt*, rgInt*> rgTest;

rgInt *pInt = new rgInt;
pInt->Add(1);
rgTest.Add(pInt);
0
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.

 
LVL 7

Expert Comment

by:leflon
ID: 10867898
gamesdev,

both CList and CArray need two arguments in the definition.
CList<int,int>
CArray<CIntList,CIntList&>

> x.Add(CList()); //Error!
will produce an error as only CIntList objects can be added to x

leflon
0
 
LVL 7

Assisted Solution

by:leflon
leflon earned 200 total points
ID: 10867934
gamesdev,

and you will (at least i got one) get an syntax error saying that list  =operator is unavailable.
so you may want to use akalmani's way or derive your own List class from CList, providing the needed =operator.

leflon
0
 
LVL 1

Author Comment

by:gamesdev
ID: 10867988
Hi leflon,
They don't, the second parameter has a default value of a reference to the first parameter. (as far as I know, I am using VS.net 2003)

And the problem is with the =operator like you mentioned, so I guess I'll use akalmani's method.

Thanks guys.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10883220
I recommend you use something like the following class:
template < class T>
class dynamic_2d_array
{
public:
      dynamic_2d_array(int row, int col):m_row(row),m_col(col), m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
      ~dynamic_2d_array(){if(m_data) delete []m_data;}
      inline T* operator[](int i) {return (m_data + (m_col*i));}
      inline T const*const operator[](int i) const {return (m_data + (m_col*i));}
private:
      const int m_row;
      const int m_col;
      T* m_data;
};

The above class is easier to use, and more efficient.
Example:
int col = 3, row = 4;
dynamic_2d_array < int > My2dArray(col, row);

My2dArray[1][2] = 99;
cout << My2dArray[1][2] << endl;
0

Featured Post

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!

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

771 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