【问题标题】:Annoying problems with struct in c++c++中struct的烦人问题
【发布时间】:2014-08-27 19:47:40
【问题描述】:

我的问题是我不知道如何像在 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' 左侧必须有类/结构/联合
错误 16 错误 C2228: '.a12' 左侧必须有类/结构/联合 错误 27 错误 C2228:“.a12”左侧必须有类/结构/联合
错误 5 错误 C2228:'.a02' 左侧必须有类/结构/联合
错误 14 错误 C2228:“.a02”左侧必须有类/结构/联合
错误 25 错误 C2228: '.a02' 左侧必须有类/结构/联合

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

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

【问题讨论】:

  • "我的问题是我不知道如何像在 c# 中那样在 c++ 中编程。"你不应该它们是完全不同的语言。
  • 好吧,我认为至少结构的东西在某种程度上是相似的。
  • Vector2 不知道 Matrix33 类,需要在 Vector2 之前转发声明或定义 Matrix33。我建议从一些 C++ 教程开始。
  • classstruct 在 C++ 中是相同的,只是 class 成员默认是私有的。
  • 人们会给你解决方案,但这并不能解决根本原因,你需要学习 C++ 基础

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


【解决方案1】:

在定义处理类 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);
}

【讨论】:

    【解决方案2】:

    您实际上不必进行很多更改。您可以转发声明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

    【讨论】:

    • 你建议的最后一行 Vector2 Matrix33::operator*(const Vector2 p) 如何获得我的 a00、a01 等值?
    • 因为代码在定义 thouse 浮点数的结构之外
    猜你喜欢
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2012-08-16
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多