Solved

Easy - Declaring a 2d array in a class.

Posted on 2007-03-22
9
184 Views
Last Modified: 2010-04-01
Hi,

class myClass{
     private:
          int testData[][]={
               {9,4,7},
               {7,9,4},
               {4,7,9};
          };
};

This is apparently missing a subscript (but if I declare the contents of the array when the variable is declared I don't have to specify the size?). With the sizes put in there is an error pointing to '{' on the line where the variable is declared. Anybody?

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
  • 4
  • 3
  • 2
9 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 18770201
When you make it static, you can do it like this :

class myClass{
     private:
          static int testData[3][3];
};

int myClass::testData[3][3] = {
                                {9,4,7},
                                {7,9,4},
                                {4,7,9}
                              };
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18770203
Also note that you had an extra ; in your initialization data ...
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18770210
Don't suppose it's doable without making it static?
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 250 total points
ID: 18770218
You can't initialize data members in a class. You need to do initialization in the constructor most preferable in the initializer list but there is no way to init an array in the initializer list. Also in the body you only can init the whole array by assigning another array:
class myClass{
     private:
          int testData[3][3];  // required to determine class size
     public:
            myClass()
            {
               static int defData[][3] ={
               {9,4,7},
               {7,9,4},
               {4,7,9},
               };
               testData = defData;
           }
};

Regards, Alex


};
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18770237
Woa... screw that idea then... static it is. Points split. :-)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18770252
>> Woa... screw that idea then... static it is.
That will only work if your data CAN be static ... ie. if it doesn't change from object instance to object instance.
Otherwise, you'd have to initialize the data in the constructor as Alex explained.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18770265
Fortunately it can be... I'm just not mad keen on having some static element in a class if I can really help it, but in this case static just seems a neater way of doing it. :)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18770322
Note, the static array within my constructor not necessarily need to be statci. However as it used as const array it gives less overhead when making it static.
Instead of defining the default array statically in the (one) constructor you may combine both methods and define the default array as a static class member.

class myClass{
     private:
          int testData[3][3];  // required to determine class size
          static int defData[3][3];
     public:
            myClass()
            {
               static int defData[][3] ={
               {9,4,7},
               {7,9,4},
               {4,7,9},
               };
               testData = defData;
           }
};

// myclass.cpp
#include "myclass.h"

int MyClass::defData[3][3] ={
               {9,4,7},
               {7,9,4},
               {4,7,9},
               };

>>>> Woa... screw that idea

In praxis fixed arrays containing const data are very common. And it makes absolutely sense to makes them static class members cause there is no need to have a copy with any new instance of the class but to have it only once. So, the inconvenience that you can set all array data only at initialization time and not for not-static members isn't so much a problem in real-life programming but more for test or educational purposes.

Regards, Alex
   
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18770431
@Alex: Thanks for the further info... :) It looks quite an interesting idea, didn't think of anything like that!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 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

11 Experts available now in Live!

Get 1:1 Help Now