455,903 Members | 1,180 Online
Need help? Post your question and get tips & solutions from a community of 455,903 IT Pros & Developers. It's quick & easy.

# two-dimentional array with non constant variables

 P: n/a I'd like to declare (in a Matrix class) a two-dimentional array with user-defined coordinates. The constructor is: Matrix(int c, int l): col(c), lin(l), a(new float[col][lin]) {} the compiler says 'lin' should be a constant, but I want it to be defined from the user, so what should I do? and how must 'a' be declared?? help me please Oct 27 '05 #1
8 Replies

 P: n/a Al wrote: I'd like to declare (in a Matrix class) a two-dimentional array with user-defined coordinates. The constructor is: Matrix(int c, int l): col(c), lin(l), a(new float[col][lin]) {} the compiler says 'lin' should be a constant, but I want it to be defined from the user, so what should I do? and how must 'a' be declared?? help me please Please consult the FAQ: http://www.parashift.com/c++-faq-lit...html#faq-16.16 But, I'd suggest you use a vector< vector > if possible. It can make life simpler in many ways. Cheers! --M Oct 27 '05 #2

 P: n/a you know... just newbie training... thx Oct 27 '05 #4

 P: n/a Al wrote: I'd like to declare (in a Matrix class) a two-dimentional array with user-defined coordinates. The constructor is: Matrix(int c, int l): col(c), lin(l), a(new float[col][lin]) {} the compiler says 'lin' should be a constant, but I want it to be defined from the user, so what should I do? and how must 'a' be declared?? You've struck right to the heart of the problem. 'a' must be declared as: float (*a)[lin]; But C++ requires that the array dimension is a compile-time constant. So it is not possible to have this array as you would like. You could either make 'a' a one-dimension array and add a function for accessing it as if it were 2-D; or you could use a vector of vectors. Oct 27 '05 #5

 P: n/a Al wrote: I'd like to declare (in a Matrix class) a two-dimentional array with user-defined coordinates. The constructor is: Matrix(int c, int l): col(c), lin(l), a(new float[col][lin]) {} the compiler says 'lin' should be a constant, but I want it to be defined from the user, so what should I do? and how must 'a' be declared?? help me please Check out the following link for an example: http://code.axter.com/dynamic_2d_array.h However, I recommend you use a vector of vector. Example: int col = 123; int row = 456; vector > My2dArray(col, vector(row)); You can reference both the above vector code and the dynamic_2d_array class using double index ([][]) My2dArray[0][0] = 99; Oct 28 '05 #6

 P: n/a thx I've never used vectors of vectors before, but it's a good reason to learn it! How do you use 'typename' ?? (from Kai-Uwe Bux: template) Oct 28 '05 #7

 P: n/a what should I do to get the size of the second dimension of the vector, without using it as a parameter? void getVector(vector >& v) { for(int i = 0;i

 P: n/a Al wrote: what should I do to get the size of the second dimension of the vector, without using it as a parameter? v[i].size() void getVector(vector >& v) { for(int i = 0;i

### This discussion thread is closed

Replies have been disabled for this discussion.