Help on passing a 2 - dimensional array to a method, I can't get itright!

P

process

http://cpp.ninjacodemonkeys.org/5016

I'm making a matrix class to better my C++ skills and hopefully later
linear algebra skills :)

Basically I'm trying to implement the commented out constructor which
takes a 2-dimensional array as an argument and copies the values to
itself.

I could pass float f[][value] where value is replaced by an int but
that is kind of against the point since I don't know that value
beforehand.
I want to dynamically create the matrix.

I have been trying to get it right with templates but don't quite
understand how one would o it.

I would really appreciate the help!
 
T

Triple-DES

http://cpp.ninjacodemonkeys.org/5016

I'm making a matrix class to better my C++ skills and hopefully later
linear algebra skills :)

Basically I'm trying to implement the commented out constructor which
takes a 2-dimensional array as an argument and copies the values to
itself.

I could pass float f[][value] where value is replaced by an int but
that is kind of against the point since I don't know that value
beforehand.
I want to dynamically create the matrix.

I have been trying to get it right with templates but don't quite
understand how one would o it.

I would really appreciate the help!


General advise: Don't post your full program, post a small example
that illustrates the problem. Is there a particular reason why you are
using float instead of double?

An easy solution is to replace the third param with a template param:
template<typename T>
Matrix::Matrix(int,int, const T& filler)
{
//...
}
 
V

Vidar Hasfjord

http://cpp.ninjacodemonkeys.org/5016

I'm making a matrix class to better my C++ skills and hopefully later
linear algebra skills :)

Basically I'm trying to implement the commented out constructor which
takes a 2-dimensional array as an argument and copies the values to
itself.

I could pass float f[][value] where value is replaced by an int but
that is kind of against the point since I don't know that value
beforehand.
I want to dynamically create the matrix.

I have been trying to get it right with templates but don't quite
understand how one would o it.

I would really appreciate the help!

To address the specific problem first: Since the size of an array is
part of its type two arrays with different dimensions are distinct
types; hence you will need a constructor for every specific dimension.
I.e. you need a template:

template <size_t N, size_t M>
Matrix::Matrix (float filler [N][M])
: rows (N), columns (M)
{/*...*/}

That said, your design is flawed. You represent the matrix as a jagged
two-dimensional structure (array of pointers to arrays), but since the
sizes of the columns in your Matrix class is the same for all columns,
a continuous memory block would be more appropriate:

float* mtrx; // Points to the start of the memory block.

Then allocate it like this:

mtrx = new float [rows * columns];

And access elements like this:

mtrx [r * columns + c] // mtrx [r][c]

Note that your current constructor is not exception safe; i.e. it will
leak memory if row allocation fails. Using a single allocation of
continuous memory solves that problem.

You also need to implement the copy constructor and assignment
operator; or disable the defaults, since these do the wrong thing for
your class.

Actually, unless you plan to allow assignment of a matrix to another
with different dimensions, you should consider making the class a
template parameterized on the dimensions:

template <size_t N, size_t M>
class Matrix {
static const size_t rows = N;
static const size_t columns = M;
float mtrx [N][M];
//...
};

Now every Matrix of unique dimensions is a distinct type and cannot be
mixed up. Also memory is now allocated as part of the type, thus
removing the need for dynamic allocation. This makes the default copy
constructor and assignment operator do the right thing as well.

Consult a book and/or search the net for C++ matrix libraries to get
more inspiration and advice.

Regards,
Vidar Hasfjord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top