-
December 10th, 2012, 03:32 AM
#1
Writing a simple copy constructor
Code:
class Matrix4
{
Matrix4(float mat[4][4])
{
memcpy (m, mat, 16);
}
float m[4][4];
};
float m[4][4];
cam->getProj(m);
Matrix4 matProj(m);
The above snippet doesn't compile, please correct my mistakes in syntax.
Thanks
Jack
-
December 10th, 2012, 04:39 AM
#2
Re: Writing a simple copy constructor
Neither cam nor getProj() is declsred.
Other errors you could get from your compiler!
Victor Nijegorodov
-
December 10th, 2012, 04:50 AM
#3
Re: Writing a simple copy constructor
Code:
class Matrix4
{
Matrix4(float mat[4][4])
This is not a copy constructor. A copy constructor takes a Matrix4 as a parameter (reference, const reference).
Second, that parameter, regardless of how many dimensions you stick inside of the [][], all degrades to a pointer. There is no guarantee that what is passed is actually has 4 rows of floats.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 10th, 2012 at 04:54 AM.
-
December 10th, 2012, 04:53 AM
#4
Re: Writing a simple copy constructor
Hello Paul,
How should I take in a float[4][4] parameter and convert it to a Matrix4?
Thanks
Jack
-
December 10th, 2012, 05:01 AM
#5
Re: Writing a simple copy constructor
Originally Posted by lucky6969b
Hello Paul,
How should I take in a float[4][4] parameter and convert it to a Matrix4?
Please post a real, compilable example. The following compiles without errors using the Comeau C++ online compiler:
Code:
#include <cstring>
class Matrix4
{
public:
Matrix4(float mat[4][4])
{
memcpy (m, mat, 16);
}
float m[4][4];
};
int main()
{
float m[4][4];
Matrix4 matProj(m);
}
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.
Regards,
Paul McKenzie
-
December 10th, 2012, 04:51 AM
#6
Re: Writing a simple copy constructor
Hello Victor,
Code:
class Matrix4
{
public:
Matrix4()
{
}
Matrix4(float mat[4][4])
{
memcpy (m, mat, 16);
}
private:
float m[4][4];
};
Matrix4 maxViewMatrix = CalculateViewMatrix(e, t, Point3(0, 0, 1));
d3dViewXform = Convert_2_D3D_Matrix(maxViewMatrix);
D3DXMATRIX Convert_2_D3D_Matrix(Matrix4 mat)
{
D3DXMATRIX m;
return m;
}
Error error C2664: the 'Convert_2_D3D_Matrix': unable to parameter 1 from 'Matrix4' convert 'float [] [4]' d: \ users \ documents \ visual studio 2010 \ projects \ simexporter \ simexporter \ simexporter.cpp 367SimExporter
Last edited by lucky6969b; December 10th, 2012 at 04:58 AM.
-
December 10th, 2012, 05:11 AM
#7
Re: Writing a simple copy constructor
Originally Posted by lucky6969b
Hello Victor,
Code:
D3DXMATRIX Convert_2_D3D_Matrix(Matrix4 mat)
{
D3DXMATRIX m;
return m;
}
Why are you passing mat by value? That line does call the copy constructor. You should be passing this by const reference.
Anyway, there is still no error with this example:
Code:
#include <cstring>
class Matrix4
{
public:
Matrix4(float mat[4][4])
{ memcpy (m, mat, 16); }
Matrix4() {}
float m[4][4];
};
void foo(Matrix4 mat)
{ }
int main()
{
float m[4][4];
Matrix4 matProj(m);
foo(m);
}
So change the code above to something that duplicates the error and will compile on everyone's system without having to introduce directX types or mystery functions that no one knows about.
Regards,
Paul McKenzie
-
December 10th, 2012, 05:23 AM
#8
Re: Writing a simple copy constructor
moreover, note that the parameter "mat" in "Matrix4(float mat[4][4])" does not have type float[4][4] as you might think. Its type is float(*)[4]; this means that you can pass an array float[1][4], float[3][4], 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.
-
December 10th, 2012, 05:36 AM
#9
Re: Writing a simple copy constructor
I'd like to pass it as reference-to-array
But I can't do this
Matrix(const float& mat[][4])
which is a syntax error
Last edited by lucky6969b; December 10th, 2012 at 05:53 AM.
-
December 10th, 2012, 08:41 AM
#10
Re: Writing a simple copy constructor
Originally Posted by lucky6969b
I'd like to pass it as reference-to-array
But I can't do this
Matrix(const float& mat[][4])
which is a syntax error
Instead of messing around with naked, dumb arrays, use std::array and you get no issues:
Code:
#include <array>
// 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;
class Matrix4
{
public:
Matrix4(Float2D& mat) : m(mat)
{ }
Matrix4() {}
Float2D m;
};
int main()
{
Float1D m1;
Float2D m;
m1.assign(10); // set the array up here
m.assign(m1);
Matrix4 matProj(m);
}
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.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 10th, 2012 at 08:43 AM.
-
December 10th, 2012, 05:08 AM
#11
Re: Writing a simple copy constructor
All problems fixed. Thanks a lot
-
December 10th, 2012, 09:43 PM
#12
Re: Writing a simple copy constructor
But How do you assign values to individual elements?
m1.assign(10); // set the array up here
Thanks
Jack
-
December 10th, 2012, 10:05 PM
#13
Re: Writing a simple copy constructor
Originally Posted by lucky6969b
But How do you assign values to individual elements?
Just like an array using [].
Regards,
Paul McKenzie
-
December 10th, 2012, 11:10 PM
#14
Re: Writing a simple copy constructor
Thanks Paul. It's all great!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|