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
Solved

Easy - Declaring a 2d array in a class.

Posted on 2007-03-22
9
208 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

837 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