为什么我的函数定义与我在 C++ 中的函数声明不兼容?

问题描述 投票:0回答:1

我有一个类叫

Matrix3x3
,定义在
LsMath.h
里,然后我在
LsMath.cpp
里定义它。

但是,Visual Studio 告诉我我的函数与声明不兼容。

LsMath.h
中,所有的运算符函数都说:

函数定义...未找到

LsMath.cpp
Matrix3x3 Matrix3x3::operator *(Matrix3x3 m, float s)
Vector3 Matrix3x3::operator *(Matrix3x3 m, Vector3 v)
中说:

声明与 [LsMath.h 中的声明]不兼容

我都试过了,但出于某种我想不通的原因,它就是行不通。

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);
}
c++ friend
1个回答
2
投票

矩阵乘以标量的运算符声明为自由函数,但定义为成员函数。

改变

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

Matrix3x3 operator *(Matrix3x3 m, float s).

此外,您从未为矩阵类定义默认构造函数。 (或 Vector3 但我认为为了简洁而省略)

© www.soinside.com 2019 - 2024. All rights reserved.