2014-08-27 31 views
0

我的问题是我不知道如何用C++编程C++中的东西。所以我不知道我的代码有什么问题。 我试图创建两个结构。一来表示Matrix33,另一个是的Vector2D:C++中令人讨厌的结构问题

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; 
    } 

}; 

的问题是,当我尝试建立我真的得到了很多有关我的矩阵中值误差修改和静态FUNC的getIdentity()。我不知道这里发生了什么。

错误7错误C2228:左 '.a12' 必须具有类/结构/联合
错误16错误C2228:左' .a12' 必须具有类/结构/联合 错误27错误C2228: '.a12'的左边必须有class/struct/union
错误5错误C2228:'.a02'的左边必须有class/struct/union
错误14错误C2228:'.a02'的左边必须有class /结构/联合
错误25错误C2228:'.a02'的左侧必须具有类/结构/联合

错误5错误C3861:'GetIden tity':标识符找不到
错误16错误C3861: 'GetIdentity':标识符找不到
错误29错误C3861: 'GetIdentity':标识符找不到
错误4错误C2653: 'Matrix33':不是类或命名空间名称
错误15错误C2653:“Matrix33”:不是类或命名空间名称
错误28错误C2653:“Matrix33”:不是类或命名空间名称

任何人都不会有一些提示,帮我在这里。我会很感激! 谢谢

+1

“我的问题是,我不知道如何在C++的东西像编程我在C#中做的。”你不应该是完全不同的语言。 – Rapptz 2014-08-27 19:50:26

+0

以及我认为至少结构的东西是simillar一些如何。 – user3430608 2014-08-27 19:52:32

+0

我现在试试 – user3430608 2014-08-27 19:52:53

回答

2

在定义处理类Matrix3的元素的类Vector2的任何成员函数之前,您必须定义类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); 
} 
1

你居然没有做出许多变化。您可以转发声明Vector2,在Matrix33之后移动Vector2的定义,然后将您的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; 
} 

Live example

+0

你建议的最后一行Vector2 Matrix33 :: operator *(const Vector2 p)会得到我的a00,a01等值吗? – user3430608 2014-08-27 20:04:55

+0

由于代码在定义thouse浮点的结构之外 – user3430608 2014-08-27 20:05:12