multi dimentional array

Hi All

i am trying following code and getting some errors


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class A
{
     const int x;
     const int y;
     const int z;
    
public:
    
    A(int a,int b, int c):x(a),y(b),z(c)
    {
        ;
    }
    
    void fun()
    {
       int(*pchar)[x][y] = new int[x][y][z];
         
        for(int i=0 ; i < 10 ; ++i)
            for(int j=0 ; j<10 ; ++j)
                for(int k=0 ; k <10 ; ++k)
                {
                    pchar[i][j][k] = i*j*k;
                }

        for(int i=0 ; i < 10 ; ++i)
        {
            for(int j=0 ; j<10 ; ++j)
            {
                for(int k=0 ; k <10 ; ++k)
                {
                    cout<<pchar[i][j][k]<<" ";
                }
            }
        }
    }
};


error:
main.cpp: In member function 'void A::fun()':
main.cpp:31:39: error: 'A::y' cannot appear in a constant-expression
make[2]: Leaving directory `D:/New Folder/CppApplication_1'
main.cpp:31:42: error: 'A::z' cannot appear in a constant-expression

however i put same code in main it works;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main(int argc, char** argv) 
{
    const int x = 10;
    const int y = 10;
    const int z = 10;
     int(*pchar)[x][y] = new int[x][y][z];
         
        for(int i=0 ; i < 10 ; ++i)
            for(int j=0 ; j<10 ; ++j)
                for(int k=0 ; k <10 ; ++k)
                {
                    pchar[i][j][k] = i*j*k;
                }

        for(int i=0 ; i < 10 ; ++i)
        {
            for(int j=0 ; j<10 ; ++j)
            {
                for(int k=0 ; k <10 ; ++k)
                {
                    cout<<pchar[i][j][k]<<" ";
                }
            }
        }
    return 0;
}


what i am doing wrong in class ?
using MINGW NetBeans with windows 7
thanks in advance




You values of x, y and z are unknown in the class definition, but you give them values in your main.

Arrays cannot be declared with unknown values.
Hi

What do you really want to do? What is the Problem ? A multidimensional Array class ?
closed account (o1vk4iN6)
It works in your main function because the values of y and z are known at compile time. For your class though they are not, they would need to be static const int and be initialized in the class. You can't define a type using unknown variables, which is what y and z are in your class but not in main.

Basically if you are defining a 3d array like that all but the first value can be unknown. So y and z need to be known by the compiler otherwise it won't work.

You can use std::vector instead to create a 3d array but i dont know how efficient that will be. That is of coarse if you don't know the size of the array otherwise you can use a c-style array depending on what you want it to do.

vector< vector< vector< int > > > array;
Last edited on
The definition of the new for arrays sets a rule for indexes

noptr-new-declarator:
[ expression ] attribute-specifier-seqopt
noptr-new-declarator [ constant-expression ] attribute-specifier-seqopt

So every index except the first must be constant-expression. And the compiler says you about this rule in your first example of code

main.cpp: In member function 'void A::fun()':
main.cpp:31:39: error: 'A::y' cannot appear in a constant-expression
make[2]: Leaving directory `D:/New Folder/CppApplication_1'
main.cpp:31:42: error: 'A::z' cannot appear in a constant-expression

That is for the statement

int(*pchar)[x][y] = new int[x][y][z];

the compiler says that used in the expression A::y and A::z must be constant expression during compilation time.

In your second example you explicitly specified constants

1
2
3
    const int x = 10;
    const int y = 10;
    const int z = 10;


So during compilation the compiler knows values of y and z. It was not nessesary to made x also constant, because you can specify any non negative value for the first index in runtime (including zero).
Last edited on
If you do want a 3D array which is not declared at compile time you will have to use a pointer-to-a-pointer-to-a-pointer:
1
2
3
4
5
6
7
8
9
char*** Array;
//Then you need to initialize the first level:
Array = new char**[x];
//Then you need to initialize each pointer in the second level:
for(int i = 0; i < x; ++i)
{
Array[i] = new char*[y];
}
//And so on 

So basically multidimensional arrays are a bitch to deal with. Of course you can also use the triple layered vector that xerzi suggested, but in my opinion the best way to do something like this is to write a simple class that handles a 1D array like a 3D one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Array3D
{
int X,Y,Z;
char* Array;
Array3D(int x,int y,int z)
{
X= x; Y = y; z = Z;
Array = new char[X*Y*Z];
}
char Access(int x,int y,int z)
{
return Array[X+Y*X+X*Y*Z];
}
}
thanks for your replies

so i have below options

1> int ***
2> boost array
3> vector
4> boost multidimensional array

Hi you also have other options

Blitz c++ library
Eigen Matrix c++ library
Hasem Matrix c++ library
MTL c++ library

Have a look first at the documentation and than decide for one :-)

hope it helps
Last edited on
Topic archived. No new replies allowed.