Possible Duplicate:
How do I use arrays in C++?
I am not sure if how to do this in C++:
main()
{
float array[5][4];
transpose(array);
}
void transpose(float** array);
=> I got some some compilation error: vector<KeypointMatch, std::allocator<KeypointMatch> >
How can I pass the variable of two dimensional array to a method in C++?
Thanks in advance.
That would be okay if you were to read or modify the positions of the matrix, however you are going to modify the matrix itself. In your example, you’ll need to create another matrix.
EDITED: I’ve modified this to use unique_ptr, because of the comments (though I don’t think the OP is really ready for/interested on this).
EDITED I know you can transpose the matrix “in-place”. That was not the question from OP, however. Thanks to all commenters about this.
std::unique_ptr<>is a class the lets you manage a pointer, assuring you that the pointer will be automatically deleted after using it. An option would beauto_ptr. Unfortunately, it it does not support vectors of arrays.unique_ptrdoes support them, though it is part of the new standard c++-0x.So, you need to modify the formal parameter of transpose to:
This also means that you cannot create the original array as you did:
This is a matrix on the stack, and you cannot expect to modify the pointer itself, it does not make sense. Better if you do:
That said, another solution is to slightly change the signature of the transpose procedure, and make it a function.
.. and you would not need to change anything else in your program.