Vector4 クラスと Matrix4x4 クラスの両方を C++ で実装して、言語をより適切に処理しようとしています。私は周りを見回しましたが、私が遭遇した問題に実際に答えているものは何もないようです。
編集:元のエラーは発生していないようです (循環包含が原因でした)。ただし、現在、次のエラーが表示されます。
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 ファイルに移動することでこれを解決することができました (これは最初に行うべきでした。なぜそうしなかったのかわかりません)。必要なヘッダーをそこに含め、ヘッダー ファイルで前方宣言を使用します。 .
これが正しい解決策かどうかはわかりませんが、機能しているようです。