Solved

Easy - Declaring a 2d array in a class.

Posted on 2007-03-22
9
220 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Industry Leaders: 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

630 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