views:

4599

answers:

4

I have a class called Cal and it's .cpp and .h counterpart

Headerfile has

class Cal {
    private:
        int wa[2][2];

    public:
        void do_cal();
};

.cpp file has

#include "Cal.h"
void Cal::do_cal() {
   print(wa) // where print just itterates and prints the elements in wa
}

My question is how do I initialize the array wa ? I just can't seem to get it to work.

I tried with :

int wa[2][2] = {
                {5,2},
                {7,9}
               };

in the header file but I get errors saying I cant do so as it's against iso..something.

Tried also to initialize the array wa in the constructor but that didnt work either.. What am I missing ?

Thanks

+7  A: 

You cannot initialize array elements in a class declaration. I recently tried to find a way to do just that. From what I learned, you have to do it in your initialize function, one element at a time.

Cal::Cal{
   wa[0][0] = 5;
   wa[0][1] = 2;
   wa[1][0] = 7;
   wa[1][1] = 9;
}

It's possible (and probable) that there's a much better way to do this, but from my research last week, this is how to do it with a multi dimensional array. I'm interested if anyone has a better method.

Perchik
+4  A: 

You can't do it easily. If you don't want to specify each element individually like in Perchik's answer, you can create one static array and memcpy that (which will probably be faster for non-trivial array sizes):

namespace
{
    const int default_wa[2][2] = {{5, 2}, {7, 9}};
}

Cal::Cal
{
    memcpy(&wa[0][0], &default_wa[0][0], sizeof(wa));
}
Adam Rosenfield
memcpy is dangerous here. If someone in the future changes the dimensions or type of one array without reflecting those changes in the other...
Mark Ransom
True, but you'd have the same problem with any other solution. You could easily throw in an assert(sizeof(wa) == sizeof(default_wa)) here to protect against that in this case.
Adam Rosenfield
You can make sure that the array sizes always match if you define them like this: `int default_wa[][2] = {{ 5, 2 }, { 7, 9 }}; int wa[count(default_wa)][count(default_wa[0])];` with `#define count(ARRAY) ((sizeof (ARRAY))/(sizeof (ARRAY[0])))`
Christoph
Good point Christoph, I've done that before. I hate #defining count() every time I need it - should have been a built-in feature.
Mark Ransom
Should also point out that you can use a static within the constructor, rather than putting the defaults in a namespace outside. But only if you don't need an inline constructor. It enhances the readability.
Mark Ransom
+7  A: 

If it can be static, you can initialize it in your .cpp file. Add the static keyword in the class declaration:

class Cal {
    private:
        static int wa[2][2];
    public:
        void do_cal();
};

and at file scope in the .cpp file add:

#include "Cal.h"
int Cal::wa[2][2] = { {5,2}, {7,9} };
void Cal::do_cal() {
   print(wa) // where print just itterates and prints the elements in wa
}

If you never change it, this would work well (along with making it const). You only get one that's shared with each instance of your class though.

Rob K
A: 

I've declare my variables in header file just like:

const char lettr[3];
const int st_end[3][2];

and wanto initialize them in cpp file:

char AbcDlg::lettr[3]={'A','B','C'};
int AbcDlg::st_end[3][2]={{0,16},{2,14},{6,10}};

what's the problem??? the error got from compiler is:

Error 1 error C2439: 'AbcDlg::lettr' : member could not be initialized c:\documents and settings\admin\desktop\abc\abc\abc\abcdlg.cpp 52 Error 3 error C2439: 'AbcDlg::list' : member could not be initialized c:\documents and settings\admin\desktop\abc\abc\abc\abcdlg.cpp 52 Error 2 error C2439: 'AbcDlg::st_end' : member could not be initialized c:\documents and settings\admin\desktop\abc\abc\abc\abcdlg.cpp 52 Error 4 error C2761: 'const char AbcDlg::lettr[3]' : member function redeclaration not allowed c:\documents and settings\admin\desktop\abc\abc\abc\abcdlg.cpp 163 Error 5 fatal error C1903: unable to recover from previous error(s); stopping compilation c:\documents and settings\admin\desktop\abc\abc\abc\abcdlg.cpp 163

PLZ help how can i initialize them???

rahhh