C2061 error: 'identifier', but I included the header?

C2061 error: 'identifier', but I included the header?

我正在做一个 VC++ 项目(没有视觉效果,只是使用 Visual Studio 进行编辑)。在我的一个 classes 中,出现了一堆 C2061 错误,但一切都很好,我进行了两次和三次检查。 这是发生错误的 class: Circle.h:

#ifndef __SGE__Circle__
#define __SGE__Circle__
#include <GLFW/glfw3.h>
#include "Vector2.h"
#include "Rectangle.h"
class Circle
{
public:
    Circle();
    Circle(float xCenter, float yCenter, float radius);
    Circle(Vector2& center, float radius);
    ~Circle();
    bool Contains(Vector2& point);
    bool Contains(Rectangle& rectangle); //ERROR OCCURS HERE
    bool Contains(Circle& circle);
    bool isContained(Rectangle& rectangle); //ERROR OCCURS HERE
    bool Intersects(Rectangle& rectangle); //ERROR OCCURS HERE
    bool Intersects(Circle& circle); 
    float Radius;
    Vector2 Center;
};
#endif

错误如下:错误 C2061:语法错误:标识符 'Rectangle' 它们随处可见 Rectangle 被称为

矩形 class 看起来像这样:

Rectangle.h:

#ifndef __SGE__Rectangle__
#define __SGE__Rectangle__
#include <GLFW/glfw3.h>
#include "Vector2.h"
class Rectangle
{
public:
    Rectangle();
    Rectangle(float x, float y, float width, float height);
    Rectangle(Vector2& position, Vector2& size);
    ~Rectangle();
    Vector2* getCorners();
    Vector2 getCenter();
    bool Contains(Vector2& point);
    bool Contains(Rectangle& rectangle);
    bool Intersects(Rectangle& rectangle);
    float X;
    float Y;
    float Width;
    float Height;
};
#endif

而且我还在 main.cpp

中导入了 Circle.h 和 Rectangle.h

为了好玩 :) Vector2.h:

#ifndef _SGE_Vector2_
#define _SGE_Vector2_
#include <GLFW/glfw3.h>
#include <math.h>
class Vector2
{
public:
    Vector2();
    Vector2(float x, float y);
    bool operator == (const Vector2& a);
    bool operator != (const Vector2& a);
    Vector2 operator +(const Vector2& a);
    Vector2 operator +=(const Vector2& a);
    Vector2 operator -(const Vector2& a);
    Vector2 operator -=(const Vector2& a);
    Vector2 operator *(const float a);
    Vector2 operator *=(const float a);
    Vector2 operator /(const float a);
    Vector2 operator /=(const float a);
    float Length();
    void Normalize();
    ~Vector2();
    GLfloat X;
    GLfloat Y;
};
#endif

"GLFW/glfw3.h" 包含一个函数:bool Rectangle(...);当使用 class 矩形时会产生错误。 此问题有 2 个解决方案:

  1. 将 class 矩形重命名为其他名称,例如:Rect
  2. 创建一个包含所有 classes 的命名空间,以便调用

using namespace X; //X is replaced by the name of your namespace

主要用 Rectangle class 覆盖 Rectangle 函数。

在Sarang的帮助下问题已经解决了!