c++ - 没有重载函数的实例 "CVector4::operator"匹配指定的类型/术语不计算为采用 2 个参数的函数

标签 c++ class matrix operator-overloading implementation

我正在尝试用 C++ 实现一个 Vector4 类和一个 Matrix4x4 类,以便更好地掌握这门语言。我环顾四周,似乎没有什么能真正解决我遇到的问题,如果我遗漏了什么,我深表歉意。

编辑:原来的错误似乎不再发生(这是由循环包含引起的)。但是,现在我收到以下错误:

1>main.cpp(35): error C2064: term does not evaluate to a function taking 2 arguments

我只能想象这是因为我在 CMatrix4x4 中重载了 () 运算符,但是在我以前的代码中从 main() 调用时并没有发生这种情况。

请求的 SSCCE 案例:

#include <assert.h>
#include <cmath>
#include <iostream>

class CMatrix4x4;

class CVector4
{
public:
    float x, y, z, w;

    CVector4();
    CVector4(float, float, float, float);
    ~CVector4();

    CVector4    operator*(CMatrix4x4&);
};

CVector4::CVector4()
{
    x, y, z, w = 0;
}
CVector4::CVector4(float cx, float cy, float cz, float cw)
{
    x = cx, y = cy, z = cz, w = cw;
}

//No instance of overloaded function "CVector4::operator" matches the specified type
//<error-type> m
//DOES NOT occur with forward declaration of class, only when including matrix.h
//from a separate file.
//Now causes "term does not evaluate to a function taking 2 arguments" at lines: 35-38
//Whenever I call the overloaded operator ()
CVector4 CVector4::operator*(CMatrix4x4& m)
{
    CVector4 v;
    v.x = x*m(0, 0) + y*m(1, 0) + z*m(2, 0) + w*m(3, 0);
    v.y = x*m(0, 1) + y*m(1, 1) + z*m(2, 1) + w*m(3, 1);
    v.z = x*m(0, 2) + y*m(1, 2) + z*m(2, 2) + w*m(3, 2);
    v.w = x*m(0, 3) + y*m(1, 3) + z*m(2, 3) + w*m(3, 3);
    return v;
}

class CMatrix4x4
{
public:
    CMatrix4x4();
    ~CMatrix4x4();

    void SetRow(int r, CVector4);

    float operator()(int r, int c);

    private:
    float   matrix4x4[4][4];
};

CMatrix4x4::CMatrix4x4()
{
    for(int r = 0; r < 4; r++)
    {
        for(int c = 0; c < 4; c++)
        {
            matrix4x4[r][c] = 0;
        }
    }
}

CMatrix4x4::~CMatrix4x4()
{
}

float CMatrix4x4::operator()(int r, int c)
{
    assert(r >= 0 && r < 4);
    assert(c >= 0 && c < 4);
    return matrix4x4[r][c];
}

void CMatrix4x4::SetRow(int r, CVector4 v)
{
    assert(r >= 0 && r < 4);
    matrix4x4[r][0] = v.x;
    matrix4x4[r][1] = v.y;
    matrix4x4[r][2] = v.z;
    matrix4x4[r][3] = v.w;
}

int main()
{
    CMatrix4x4 m;
    CVector4 vec1(1, 2, 3, 4);
    CVector4 vec2;

    m.SetRow(0, CVector4(1, 0, 0, 0));
    m.SetRow(1, CVector4(0, 1, 0, 0));
    m.SetRow(2, CVector4(0, 0, 1, 0));
    m.SetRow(3, CVector4(0, 0, 0, 1));
    vec2 = vec1 * m;
    std::cout << vec2.x;
    std::cin.ignore();
    return 0;
}

编辑:感谢所有提供帮助的人。我设法通过将函数实现移动到单独的 .cpp 文件来解决这个问题(我应该首先这样做。我不知道为什么我不这样做),并在其中包含所需的 header ,并在 header 文件中使用前向声明.

我不确定这是否是正确的解决方案,但它看起来确实有效。

最佳答案

与之前提出的许多问题相同的问题:代码的原始版本显然有两个头文件 - vector.hmatrix.h - 它们相互包含.这是循环包含,没有任何意义。

您的头文件中可能包含的 include guards 确保包含不会变成无限的。但是它们对解决数据类型之间的循环依赖没有任何作用。 CMatrix4x4 在您的 vector.h 中是完全未知的,这会导致错误。

vector.hCMatrix4x4 的前向声明是朝着正确方向迈出的一步。但是,您无论如何都必须摆脱那个无用的循环包含。你必须记住 CMatrix4x4vector.h 中是一个不完整的类型,这意味着你将无法在 vector 中访问它的内部结构。 h.

后者意味着您的 CVector4 CVector4::operator*(CMatrix4x4& m) 必须在CMatrix4x4 的定义之后定义,而不是前。在您的代码中,它在 CMatrix4x4 之前定义。此时 CMatrix4x4 类型仍然不完整,这意味着您不能使用它的 () 运算符。像 m(0, 0) 这样的表达式不会因为这个原因而被编译。这就是您收到错误的原因。

附言此外,

x, y, z, w = 0;

并不像您认为的那样。它会将 0 分配给 w,但保持其他数据成员不变(阅读 C++ 中的逗号运算符)。

关于c++ - 没有重载函数的实例 "CVector4::operator"匹配指定的类型/术语不计算为采用 2 个参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14166169/

相关文章:

C++ 从虚拟键从 A 到 Z

c++ - 多个 DLL 写入同一个文本文件?

iphone - #import 和@class 的问题导致从错误中引用 _OBJC_CLASS_

R:如何将矩阵每一行中的条目加倍并插入新行

c++ - openframeworks - 如何淡入淡出视频

c++ - 静态变量和线程局部存储

python - 如何使用 IPython %lprun 魔术函数分析类方法

oop - 类与数据结构

c++ - 加速矩阵乘法

c++ - 构造一个空矩阵