-3

I have a class called Matrix3x3, which is defined in LsMath.h, and then I define it in LsMath.cpp.

However, Visual Studio tells me that my function is incompatible with the declaration.

In LsMath.h, all of the operator functions say:

Function definition for ... not found

And in LsMath.cpp, Matrix3x3 Matrix3x3::operator *(Matrix3x3 m, float s) and Vector3 Matrix3x3::operator *(Matrix3x3 m, Vector3 v) say:

Declaration is incompatible with [The declaration in LsMath.h]

I've tried it all, but for some reason that I can't figure out, it just won't work.

LsMath.h

class Matrix3x3 {
public:
    float xx;
    float xy;
    float xz;
    float yx;
    float yy;
    float yz;
    float zx;
    float zy;
    float zz;

    Matrix3x3();
    Matrix3x3(float _xx, float _xy, float _xz, float _yx, float _yy, float _yz, float _zx, float _zy, float _zz);

    friend Matrix3x3 operator *(Matrix3x3 m, float s);
    friend Matrix3x3 operator *(float s, Matrix3x3 m);

    friend Matrix3x3 operator /(Matrix3x3 m, float s);
    friend Matrix3x3 operator /(float s, Matrix3x3 m);

    friend Vector3 operator *(Matrix3x3 m, Vector3 v);
};

Matrix3x3 operator *(Matrix3x3 m, float s);
Matrix3x3 operator *(float s, Matrix3x3 m);

Matrix3x3 operator /(Matrix3x3 m, float s);
Matrix3x3 operator /(float s, Matrix3x3 m);

Vector3 operator *(Matrix3x3 m, Vector3 v);

LsMath.cpp

Matrix3x3::Matrix3x3(float _xx, float _xy, float _xz, float _yx, float _yy, float _yz, float _zx, float _zy, float _zz) {
    xx = _xx;
    xy = _xy;
    xz = _xz;
    yx = _yx;
    yy = _yy;
    yz = _yz;
    zx = _zx;
    zy = _zy;
    zz = _zz;
}

Matrix3x3 Matrix3x3::operator *(Matrix3x3 m, float s) {
    Matrix3x3 out = Matrix3x3();
    out.xx = s * m.xx;
    out.xy = s * m.xy;
    out.xz = s * m.xz;
    out.yx = s * m.yx;
    out.yy = s * m.yy;
    out.yz = s * m.yz;
    out.zx = s * m.zx;
    out.zy = s * m.zy;
    out.zz = s * m.zz;
    return out;
}

Matrix3x3 operator *(float s, Matrix3x3 m) {
    return m * s;
}

Matrix3x3 operator /(Matrix3x3 m, float s) {
    Matrix3x3 out = Matrix3x3();
    out.xx = s / m.xx;
    out.xy = s / m.xy;
    out.xz = s / m.xz;
    out.yx = s / m.yx;
    out.yy = s / m.yy;
    out.yz = s / m.yz;
    out.zx = s / m.zx;
    out.zy = s / m.zy;
    out.zz = s / m.zz;
    return out;
}

Matrix3x3 operator /(float s, Matrix3x3 m) {
    return m / s;
}

Vector3 Matrix3x3::operator *(Matrix3x3 m, Vector3 v) {
    return Vector3(m.xx * v.x + m.xy * v.y + m.xz * v.z, m.yx * v.x + m.yy * v.y + m.yz * v.z, m.zx * v.x + m.zy * v.y + m.zz * v.z);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

2

The operator for multiplying a matrix by a scalar is declared as a free function but defined as a member function.

Change

Matrix3x3 Matrix3x3::operator *(Matrix3x3 m, float s)

to

Matrix3x3 operator *(Matrix3x3 m, float s).

Also you never defined the default constructor for the matrix class. (Or Vector3 but I assume that was omitted for concision)

jwezorek
  • 8,592
  • 1
  • 29
  • 46