c ++中struct的恼人问题

时间:2014-08-27 19:47:40

标签: c++ visual-c++ struct compiler-errors

我的问题是,我不知道如何使用c#编写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'必须有class / struct / union
  错误16错误C2228:左边的' .a12'必须有class / struct / union   错误27错误C2228:左边的' .a12'必须有class / struct / union
  错误5错误C2228:左边的' .a02'必须有class / struct / union
  错误14错误C2228:左边的' .a02'必须有class / struct / union
  错误25错误C2228:左边的' .a02'必须有class / struct / union

     

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

有没有人有一些提示可以帮助我。我很感激! 感谢

2 个答案:

答案 0 :(得分: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 :(得分:1)

您实际上无需进行太多更改。您可以转发声明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;
}

Live example