Whenever you get a compiler error, it is imperative that you post a valid example. Your example contained numerous errors, and who knows if one error triggers other errors that are unrelated. For example, your constructor is not public, so how can you call it from any function? I made it public, added the appropriate headers, a main() program, and everything compiles successfully.
moreover, note that the parameter "mat" in "Matrix4(float mat)" does not have type float as you might think. Its type is float(*); this means that you can pass an array float, float, etc... to that constructor without any complaint from the compiler but with disastrous effects at runtime.
If you want type checking either pass by reference-to-array or properly write a copy constructor, as suggested by Paul.
I'd like to pass it as reference-to-array
But I can't do this
Matrix(const float& mat)
which is a syntax error
Instead of messing around with naked, dumb arrays, use std::array and you get no issues:
// use std::array if using a more modern compiler instead of std::tr1::array
typedef std::tr1::array<float, 4> Float1D;
typedef std::tr1::array<Float1D, 4> Float2D;
Matrix4(Float2D& mat) : m(mat)
m1.assign(10); // set the array up here
This solves the issue of passing by reference, and also addresses the disastrous effects if somewhere in your code you have declared anything but 4 rows and you inadvertently pass it to your functions. The std::array class is a lightweight STL class that wraps an array declaration. So all that's done is to create an array of an array.
If your Matrix class only consists of a 4x4 float array, all you really need is std::array as outlined above, and just typedef it to Matrix4.
Last edited by Paul McKenzie; December 10th, 2012 at 07:43 AM.