【发布时间】:2020-05-20 20:15:14
【问题描述】:
我正在尝试实现自定义 Vector2(2D 向量)和 Matrix2(2x2 矩阵)类。我想定义一个对向量执行仿射变换的函数,就好像它是二维空间中的一个点。为此,我需要传递变换矩阵、可选的变换原点和可选的平移。
我希望这个函数成为我的 Vector2 类的成员。但是,我对 Matrix2 的定义位于定义此函数的 Vector2 定义的下方。因此,当我尝试编译代码时出现编译错误:
C2061 语法错误:标识符“Matrix2”
我的 Matrix2 定义包括一个将矩阵乘以向量的函数,所以我需要在 Vector2 之后定义 Matrix2。
这里是代码,省略了不相关的成员:
#pragma once
namespace AWUtils
{
struct Vector2
{
double x;
double y;
// default constructor, produces the zero vector
Vector2()
{
zero();
}
// copy constructor, copies the given vector
Vector2(const Vector2* v)
{
x = v->x;
y = v->y;
}
// turns this vector into the zero vector
void zero()
{
x = 0;
y = 0;
}
// returns a new vector equal to this vector plus the given vector
Vector2 add(Vector2 v)
{
return Vector2(x + v.x, y + v.y);
}
Vector2 operator + (Vector2 v)
{
return add(v);
}
Vector2 operator - (Vector2 v)
{
return add(v * -1);
}
// affine transformation
Vector2 transform(Matrix2 matrix, Vector2 origin = Vector2(), Vector2 translation = Vector2())
{
Vector2 me(this);
return (matrix * (me + translation - origin)) + origin;
}
};
struct Matrix2
{
double a;
double b;
double c;
double d;
// vector multiplication
Vector2 multiply(Vector2 v)
{
return Vector2(a * v.x + b * v.y, c * v.x + d * v.y);
}
Vector2 operator * (Vector2 v)
{
return multiply(v);
}
};
}
我必须在别处定义这个函数吗?我考虑过在 Matrix2 下定义它,但我更希望它是一个成员函数。任何帮助(包括代码批评,我已经做了不到两周的 C++)将不胜感激。
【问题讨论】:
-
看起来罪魁祸首是
Vector2::transform。我会将其移出Vector2作为独立功能。您也许可以将该功能楔入Matrix2,但我不建议这样做。