Solved

Easy - Declaring a 2d array in a class.

Posted on 2007-03-22
9
211 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
Independent Software Vendors: 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!

 
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
 
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

Independent Software Vendors: 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

679 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