0

我有这个构造函数:

Transform::Transform( float matrix[4][4] )
{
    m = matrix;
}

而这个类定义:

class Transform
{
    float m[4][4];
public:
    Transform();
    Transform(float matrix[4][4]);

但这不会编译。

有什么问题?

错误 1 ​​错误 C2440: '=' : 无法从 'float [][4]' 转换为 'float [4][4]' c:\Users\Josh\Documents\agui\trunk\src\Agui\Transform.cpp 75

谢谢

4

3 回答 3

4

如果您使用的是 c++11,请尝试更改float matrix[4][4]std::array<std::array<float,4>,4>

有点拗口,但它支持 c 数组本身不支持的此类操作。

你可以做这样的事情来清理语法。

typedef std::array<std::array<float,4>,4> Matrix;

现在你可以做

Matrix myMatrix;

ps 如果您不使用 C++11,则可以vector使用array. 它与数组略有不同,但也添加了更多功能,并且在您设置后访问是相同的。

于 2013-01-09T02:08:29.903 回答
3

Karthik 的回答非常好,或者,您也可以这样做...

  for(int i = 0; i < 4; i++)
  {
     for(int j = 0; j < 4; j++)
      {
         m[i][j] = matrix[i][j];
      }
  }

原理与 WhozCraig 在评论中提到的相同。

于 2013-01-09T02:11:09.393 回答
1

即使您将构造函数的参数声明为float matrix[4][4],编译器也会忽略第一个4

于 2013-01-09T02:14:39.700 回答