我刚刚实现了自己的矩阵类,它有一些函数,我写了=
和*
运算符重载函数来复制和乘法矩阵,但是当我运行一个测试程序时,我得到了以下错误:
二进制表达式的操作数无效(Matrix4X4和Matrix4X4)
我的主要代码:
#include <iostream>
#include "Matrix4X4.h"
using namespace std;
int main(int argc, const char * argv[])
{
Matrix4X4 mat(4);
Matrix4X4 sat;
Matrix4X4 result;
sat.setTranslation(5, 2, 6);
//////////// just printing code nothing worth looking for ///////////////////////////
float * ptr = mat.getMat();
for (int i = 0 ; i < 16; i += 4)
cout << ptr[i] << " " << ptr[i+1] << " " << ptr[i+2] << " " << ptr[i+3] << endl;
cout << "\n\n\n" << endl;
ptr = sat.getMat();
for (int i = 0 ; i < 16; i += 4)
cout << ptr[i] << " " << ptr[i+1] << " " << ptr[i+2] << " " << ptr[i+3] << endl;
////////////////////////////// end of printing code //////////////////////////////////////
result = mat * sat; // <<<< line that generates the error.
return 0;
}
Matrix4X4.cpp文件:
#include "Matrix4X4.h"
Matrix4X4::Matrix4X4()
{
setIdentity();
}
Matrix4X4::Matrix4X4(float value)
{
for(int i = 0 ; i < 4; i++)
for ( int j = 0; j < 4; j++)
Matrix[i][j] = value;
}
Matrix4X4::~Matrix4X4()
{
}
void Matrix4X4::setIdentity()
{
Matrix[0][0] =1; Matrix[0][1] = 0; Matrix[0][2] = 0; Matrix[0][3] = 0;
Matrix[1][0] =0; Matrix[1][1] = 1; Matrix[1][2] = 0; Matrix[1][3] = 0;
Matrix[2][0] =0; Matrix[2][1] = 0; Matrix[2][2] = 1; Matrix[2][3] = 0;
Matrix[3][0] =0; Matrix[3][1] = 0; Matrix[3][2] = 0; Matrix[3][3] = 1;
}
void Matrix4X4::setTranslation(float x,float y,float z)
{
Matrix[0][0] =1; Matrix[0][1] = 0; Matrix[0][2] = 0; Matrix[0][3] = x;
Matrix[1][0] =0; Matrix[1][1] = 1; Matrix[1][2] = 0; Matrix[1][3] = y;
Matrix[2][0] =0; Matrix[2][1] = 0; Matrix[2][2] = 1; Matrix[2][3] = z;
Matrix[3][0] =0; Matrix[3][1] = 0; Matrix[3][2] = 0; Matrix[3][3] = 1;
}
float * Matrix4X4::getMat()
{
return (float *) Matrix;
}
float Matrix4X4::getMember(int x, int y)
{
return Matrix[x][y];
}
void Matrix4X4::setMat(int row,int col,float value)
{
Matrix[row][col] = value;
}
Matrix4X4 operator * (Matrix4X4 & lhs,Matrix4X4 & rhs)
{
Matrix4X4 result;
for(int i = 0 ; i < 4; i++)
for ( int j = 0; j < 4; j++)
result.setMat(i, j, lhs.getMember(i,0) * rhs.getMember(0, j) +
lhs.getMember(i,1) * rhs.getMember(1, j) +
lhs.getMember(i,2) * rhs.getMember(2, j) +
lhs.getMember(i,3) * rhs.getMember(3, j));
return result;
}
Matrix4X4.h文件:
#ifndef ___D_GameDev_Kit__Matrix4X4__
#define ___D_GameDev_Kit__Matrix4X4__
#include <iostream>
class Matrix4X4
{
private:
float Matrix[4][4];
public:
Matrix4X4();
Matrix4X4(float);
~Matrix4X4();
void setIdentity();
void setTranslation(float,float,float);
void setRotation();
void setScale(float,float,float);
void setPerspective();
void setOrthogonal();
float * getMat();
float getMember(int,int);
void setMat(int,int,float);
Matrix4X4 & operator = (Matrix4X4 & rhs)
{
if(this!=&rhs)
for(int i = 0 ; i < 4; i++)
for (int j = 0; j < 4; j++)
Matrix[j][i] = rhs.getMember(j, i);
return *this;
}
};
#endif /* defined(___D_GameDev_Kit__Matrix4X4__) */
答案 0 :(得分:2)
看起来你已经正确地实现了操作符,但是你忘了为它添加一个前向声明。
将此行添加到Matrix4X4.h
文件中以解决此问题:
Matrix4X4 operator * (Matrix4X4 & lhs, Matrix4X4 & rhs);
需要在之外添加类声明,因为它是一个独立的二元运算符。
请注意,您可以通过将float getMember(int,int);
标记为常量,并声明lhs
和rhs
参数const
引用来改进代码:
float getMember(int,int) const;
Matrix4X4 & operator = (const Matrix4X4 & rhs) // Make your assignment operator take a const
...
Matrix4X4 operator * (const Matrix4X4 &lhs, const Matrix4X4& rhs);
答案 1 :(得分:2)
包含函数main的模块对operator *
一无所知,因为模块中包含的标头没有运算符的声明。您必须在标题中包含运算符的声明。
此外,运营商应声明为
Matrix4X4 operator * ( const Matrix4X4 & lhs, const Matrix4X4 & rhs);
复制赋值运算符也应该以与参数应该具有限定符const
相同的方式声明Matrix4X4 & operator = ( const Matrix4X4 & rhs );
答案 2 :(得分:0)
*运算符也可以是成员:
Matrix4X4 operator *(const Matrix4X4& rhs);
和lhs
将为(*this)
。
在这两种情况下,它都应出现在标题中。