J
jayharris
I'm having a ton of trouble initializing a multi-dimensional array
inside a constructor, largely because I don't know the size of the
array until runtime. I have a class that looks like this:
class MyClass
{
public:
const int size;
MyClass( const int );
};
MyClass::MyClass( const int s )
: size( s )
{
int (*arrayPtr)[size][size] = new int[size][size][size];
}
When I compile this with g++ (actually, Apple's version of g++, in case
that matters), I get the following error:
myclass.cpp : In constructor 'MyClass::MyClass(int)':
myclass.cpp.8: error: 'MyClass::size' cannot appear in a
constant-expression
myclass.cpp.8: error: 'MyClass::size' cannot appear in a
constant-expression
Can anyone explain why I'm getting this error message, especially since
size is declared const, and also can anyone help me out with what to do
about it? Thanks.
--Jay
inside a constructor, largely because I don't know the size of the
array until runtime. I have a class that looks like this:
class MyClass
{
public:
const int size;
MyClass( const int );
};
MyClass::MyClass( const int s )
: size( s )
{
int (*arrayPtr)[size][size] = new int[size][size][size];
}
When I compile this with g++ (actually, Apple's version of g++, in case
that matters), I get the following error:
myclass.cpp : In constructor 'MyClass::MyClass(int)':
myclass.cpp.8: error: 'MyClass::size' cannot appear in a
constant-expression
myclass.cpp.8: error: 'MyClass::size' cannot appear in a
constant-expression
Can anyone explain why I'm getting this error message, especially since
size is declared const, and also can anyone help me out with what to do
about it? Thanks.
--Jay