I am trying to implement a Matrix4x4 class for my port of 3D Engine that I had made earlier. Here is what I have so far in my header file:
我正在尝试为我之前制作的3D引擎端口实现Matrix4x4类。这是我目前在头文件中的内容:
#ifndef MAT4_H
#define MAT4_H
class Matrix4
{
public:
Matrix4() {}
float[4][4] getMatrix() { return m; }
//...
//other matrix related methods are omitted
//...
private:
float m[4][4];
};
#endif
But the method that is supposed to return the two-dimensional array causes this error:
但是应该返回二维数组的方法会导致此错误:
src/Matrix4.h:13:10: error: expected unqualified-id before '[' token
float[4][4] getMatrix() { return m; }
^
I am sorry if this question already has an answer, but the answers that I found on this site were usually about returning pointers instead of an array. Hope you can help, thanks.
我很抱歉,如果这个问题已经有了答案,但我在这个网站上找到的答案通常是关于返回指针而不是数组。希望你能帮忙,谢谢。
3 个解决方案
#1
9
I would suggest to use std::array
. But using it directly in code, as multi array, is a bit ugly. So I'd suggest an alias, defined as:
我建议使用std :: array。但是直接在代码中使用它作为多数组,有点难看。所以我建议别名,定义为:
#include <array>
namespace details
{
template<typename T, std::size_t D, std::size_t ... Ds>
struct make_multi_array
: make_multi_array<typename make_multi_array<T,Ds...>::type, D> {};
template<typename T, std::size_t D>
struct make_multi_array<T,D> { using type = std::array<T, D>; };
}
template<typename T, std::size_t D, std::size_t ... Ds>
using multi_array = typename details::make_multi_array<T,D,Ds...>::type;
Then use it as:
然后用它作为:
public:
multi_array<float,4,4> getMatrix() { return m; }
private:
multi_array<float,4,4> m;
You could use the alias in other places as well, such as:
您也可以在其他地方使用别名,例如:
//same as std::array<int,10>
//similar to int x[10]
multi_array<int,10> x;
//same as std::array<std::array<int,20>,10>
//similar to int y[10][20]
multi_array<int,10,20> y;
//same as std::array<std::array<std::array<int,30>,20>,10>
//similar to int z[10][20][30]
multi_array<int,10,20,30> z;
Hope that helps.
希望有所帮助。
#2
0
Passing an array in either C or C++ is possible by a passing pointer to its first element- the pointer is passed by value.
通过指向其第一个元素的指针,可以在C或C ++中传递数组 - 指针通过值传递。
The only way to pass your array by value would be to encapsulate it in a struct, but in most cases its better to pass a pointer then to copy all the data by value.
按值传递数组的唯一方法是将其封装在结构中,但在大多数情况下最好传递指针然后按值复制所有数据。
#3
-1
Just return a pointer to the first element of the array! :)
只需返回指向数组第一个元素的指针! :)
#1
9
I would suggest to use std::array
. But using it directly in code, as multi array, is a bit ugly. So I'd suggest an alias, defined as:
我建议使用std :: array。但是直接在代码中使用它作为多数组,有点难看。所以我建议别名,定义为:
#include <array>
namespace details
{
template<typename T, std::size_t D, std::size_t ... Ds>
struct make_multi_array
: make_multi_array<typename make_multi_array<T,Ds...>::type, D> {};
template<typename T, std::size_t D>
struct make_multi_array<T,D> { using type = std::array<T, D>; };
}
template<typename T, std::size_t D, std::size_t ... Ds>
using multi_array = typename details::make_multi_array<T,D,Ds...>::type;
Then use it as:
然后用它作为:
public:
multi_array<float,4,4> getMatrix() { return m; }
private:
multi_array<float,4,4> m;
You could use the alias in other places as well, such as:
您也可以在其他地方使用别名,例如:
//same as std::array<int,10>
//similar to int x[10]
multi_array<int,10> x;
//same as std::array<std::array<int,20>,10>
//similar to int y[10][20]
multi_array<int,10,20> y;
//same as std::array<std::array<std::array<int,30>,20>,10>
//similar to int z[10][20][30]
multi_array<int,10,20,30> z;
Hope that helps.
希望有所帮助。
#2
0
Passing an array in either C or C++ is possible by a passing pointer to its first element- the pointer is passed by value.
通过指向其第一个元素的指针,可以在C或C ++中传递数组 - 指针通过值传递。
The only way to pass your array by value would be to encapsulate it in a struct, but in most cases its better to pass a pointer then to copy all the data by value.
按值传递数组的唯一方法是将其封装在结构中,但在大多数情况下最好传递指针然后按值复制所有数据。
#3
-1
Just return a pointer to the first element of the array! :)
只需返回指向数组第一个元素的指针! :)