C2061 错误:"标识符",但我包含标头?

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

本文关键字:包含标 错误 标识符 C2061      更新时间:2023-10-16

我正在做一个VC++项目(没有什么可视化的,只是使用Visual Studio进行编辑)。在我的一堂课上,我有一堆 C2061 错误,但 evreything 很好,我仔细检查了三重检查。这是发生错误的类:圆圈:

#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:语法错误:标识符"矩形"他们到处都是矩形被称为矩形

矩形类如下所示:

矩形.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

我还在我的主目录中导入了 Circle.h 和 Rectangle.h.cpp

为了好玩:)矢量2.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(...); 当使用类 Rectangle 时会产生错误。 此问题有 2 种解决方案:

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

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

在主要用矩形类覆盖矩形函数中。

在萨朗的帮助下,问题得到了解决!