Моя проблема в том, что я не знаю, как программировать вещи в c++, как в С#. Поэтому я не знаю, в чем проблема с моим кодом. Я пытаюсь создать две структуры. Один из них представляет Matrix33, а другой - vector2d:
struct Vector2
{
float x;
float y;
float h;
Vector2()
{
x = y = h = 0;
}
Vector2(float valueX, float valueY, float valueH)
{
x = valueX;
y = valueY;
h = valueH;
}
Vector2(float valueX, float valueY)
{
x = valueX;
y = valueY;
h = 1;
}
Vector2 Translate(Vector2 dir)
{
Matrix33 matrix = Matrix33::GetIdentity();
matrix.a02 = dir.x;
matrix.a12 = dir.y;
return matrix * Vector2(x, y, h);
}
static Vector2 VectorOne()
{
return Vector2(1, 1, 1);
}
static Vector2 VectorUP()
{
return Vector2(0, 1, 0);
}
static Vector2 VectorFoward()
{
return Vector2(0, 0, 1);
}
static Vector2 VectorRight()
{
return Vector2(1, 0, 0);
}
Vector2 operator*(const int value)
{
return Vector2(x*value, y*value, h);
}
Vector2 operator/(const int value)
{
return Vector2(x / value, y / value, 1);
}
Vector2 operator+(const Vector2 value)
{
return Vector2(x + value.x, y + value.y);
}
Vector2 operator-(const Vector2 value)
{
return Vector2(x - value.x, y - value.y);
}
};
struct Matrix33
{
float a00; float a01; float a02;
float a10; float a11; float a12;
float a20; float a21; float a22;
Matrix33()
{
a00 = a01 = a02 = a10 = a11 = a12 = a20 = a21 = a22 = 0;
}
void AddValues(int line, float c1, float c2, float c3)
{
if (line == 0)
{
a00 = c1;
a01 = c2;
a02 = c3;
}
else if (line == 1)
{
a10 = c1;
a11 = c2;
a12 = c3;
}
else if (line == 2)
{
a20 = c1;
a21 = c2;
a20 = c3;
}
}
static Matrix33 GetIdentity()
{
Matrix33 m;
m.a00 = m.a11 = m.a22 = 1;
return m;
}
Matrix33 operator*(const Matrix33 m)
{
Matrix33 result;
result.AddValues(0, (a00*m.a00 + a01*m.a10 + a02*m.a20), (a00*m.a01 + a01*m.a11 + a02*m.a21), (a00*m.a02 + a01*m.a12 + a02*m.a22));
result.AddValues(1, (a10*m.a00 + a11*m.a10 + a12*m.a20), (a10*m.a01 + a11*m.a11 + a12*m.a21), (a10*m.a02 + a11*m.a12 + a12*m.a22));
result.AddValues(2, (a20*m.a00 + a21*m.a10 + a22*m.a20), (a20*m.a01 + a21*m.a11 + a22*m.a21), (a20*m.a02 + a21*m.a12 + a22*m.a22));
return result;
}
Vector2 operator*(const Vector2 p)
{
Vector2 result;
result.x = a00*p.x + a01*p.y + a02*p.h;
result.y = a10*p.x + a11*p.y + a12*p.h;
result.h = a20*p.x + a21*p.y + a22*p.h;
return result;
}
};
Проблема в том, что когда я пытаюсь построить, я получаю много эрросов, связанных с моими матричными значениями aN и статическими функциями GetIdentity(). Я понятия не имею, что здесь происходит.
Ошибка 7 ошибки C2228: слева от '.a12' должен быть класс /struct/union
Ошибка 16 ошибки C2228: слева от '.a12' должен быть класс /struct/union Ошибка 27 ошибки C2228: слева от '.a12' должен быть класс /struct/union
Ошибка ошибки 5 C2228: слева от '.a02' должен быть класс/структура/объединение
Ошибка 14 ошибки C2228: слева от '.a02' должен быть класс /struct/union
Ошибка 25 Ошибка C2228: слева от '.a02' должен быть класс /struct/unionОшибка 5 ошибки C3861: "GetIdentity": идентификатор не найден
Ошибка 16 ошибки C3861: "GetIdentity": идентификатор не найден
Ошибка 29 ошибки C3861: "GetIdentity": идентификатор не найден
Ошибка 4 ошибки C2653: "Matrix33": не является именем класса или пространства имен
Ошибка 15 ошибки C2653: "Matrix33": не является именем класса или пространства имен
Ошибка 28 ошибки C2653: "Matrix33": не является именем класса или пространства имен
У кого-нибудь есть несколько советов, которые помогут мне здесь. Я был бы очень признателен! благодаря
Перед определением любой функции-члена класса Vector2, которая имеет дело с элементами класса Matrix3, вы должны определить класс Matrix3. И только после определения Matrix3 вы можете определить эти функции-члены класса Vector2.
Например
struct Vector2
{
float x;
float y;
float h;
Vector2()
{
x = y = h = 0;
}
Vector2(float valueX, float valueY, float valueH)
{
x = valueX;
y = valueY;
h = valueH;
}
Vector2(float valueX, float valueY)
{
x = valueX;
y = valueY;
h = 1;
}
// not defined but only declared
Vector2 Translate(Vector2 dir);
// other members of the class
};
struct Matrix33
{
float a00; float a01; float a02;
float a10; float a11; float a12;
float a20; float a21; float a22;
Matrix33()
{
a00 = a01 = a02 = a10 = a11 = a12 = a20 = a21 = a22 = 0;
}
void AddValues(int line, float c1, float c2, float c3)
{
if (line == 0)
{
a00 = c1;
a01 = c2;
a02 = c3;
}
else if (line == 1)
{
a10 = c1;
a11 = c2;
a12 = c3;
}
else if (line == 2)
{
a20 = c1;
a21 = c2;
a20 = c3;
}
}
static Matrix33 GetIdentity()
{
Matrix33 m;
m.a00 = m.a11 = m.a22 = 1;
return m;
}
Matrix33 operator*(const Matrix33 m)
{
Matrix33 result;
result.AddValues(0, (a00*m.a00 + a01*m.a10 + a02*m.a20), (a00*m.a01 + a01*m.a11 + a02*m.a21), (a00*m.a02 + a01*m.a12 + a02*m.a22));
result.AddValues(1, (a10*m.a00 + a11*m.a10 + a12*m.a20), (a10*m.a01 + a11*m.a11 + a12*m.a21), (a10*m.a02 + a11*m.a12 + a12*m.a22));
result.AddValues(2, (a20*m.a00 + a21*m.a10 + a22*m.a20), (a20*m.a01 + a21*m.a11 + a22*m.a21), (a20*m.a02 + a21*m.a12 + a22*m.a22));
return result;
}
Vector2 operator*(const Vector2 p)
{
Vector2 result;
result.x = a00*p.x + a01*p.y + a02*p.h;
result.y = a10*p.x + a11*p.y + a12*p.h;
result.h = a20*p.x + a21*p.y + a22*p.h;
return result;
}
};
// now you may define it
Vector2 Vector2::Translate(Vector2 dir)
{
Matrix33 matrix = Matrix33::GetIdentity();
matrix.a02 = dir.x;
matrix.a12 = dir.y;
return matrix * Vector2(x, y, h);
}
На самом деле вам не нужно много менять. Вы можете переслать объявление Vector2
, переместить определение Vector2
после Matrix33
, а затем переместить определение вашего operator*
вне Matrix33
. Например:
// Forward declaration
struct Vector2;
struct Matrix33
{
// ...
Vector2 operator*(const Vector2 p);
// ^ declaration only because
// Vector2 is an incomplete type at this point
};
struct Vector2
{
// ...
};
Vector2 Matrix33::operator*(const Vector2 p)
{
Vector2 result;
result.x = a00*p.x + a01*p.y + a02*p.h;
result.y = a10*p.x + a11*p.y + a12*p.h;
result.h = a20*p.x + a21*p.y + a22*p.h;
return result;
}