【问题标题】:What's wrong here? - ISO C++ forbids declaration of 'Circle' with no type这里有什么问题? - ISO C++ 禁止声明没有类型的“Circle”
【发布时间】:2010-07-28 19:31:46
【问题描述】:

也许你可以帮我解决这个问题。 我有一个用于画圆的类,但编译器向我发送了这条消息:

In file included from ./Includes.h:19,
                 from ./Circle.h:8,
                 from ./Circle.cpp:5:
./GameApp.h:24: error: ISO C++ forbids declaration of 'Circle' with no type
./GameApp.h:24: error: expected ';' before '*' token

这里是 GameApp.h:

#include "Includes.h"

class GameApp {

public:
 GameApp();
 ~GameApp();
 void Render();

protected:
 void InitGU();
 bool Controls();

 void *dList;  // display List, used by sceGUStart
 void *fbp0;  // frame buffer

 Circle* circle;
};

Include.h 看起来像这样:

//************************************************************************
//                              Includes.h
//************************************************************************

#include <malloc.h>     //For memalign()
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdio.h>
#include <psprtc.h>             // for the timer/fps functions
#include <pspctrl.h>
#include <math.h>

// GU
#include <pspgu.h>
#include <pspgum.h>

// Custom
#include "GameApp.h"
#include "Circle.h"


//************************************************************************

// Definitions
#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)

#define sin_d(x) (sin((x)*M_PI/180))
#define cos_d(x) (cos((x)*M_PI/180))
#define tan_d(x) (tan((x)*M_PI/180)) 

//************************************************************************

// structs, datatypes...
#ifndef VERTEX_TYPE_
#define VERTEX_TYPE_
typedef struct {
    unsigned int color;
    float x, y, z;
} vertex;
#endif

还有 Circle.h

//************************************************************************
//                              Circle.h
//************************************************************************

#ifndef CIRCLE_H_
#define CIRCLE_H_

#include "Includes.h"

class Circle {

public:
    Circle(float r1);
    ~Circle();
    void Render();

    float r;
    float x, y;
    float vx, vy;

protected:
    vertex* vertices;
    int n;

};

#endif

【问题讨论】:

  • 我删除了 C 标签。把C放在这样的问题上是非常荒谬的。而且,这门课也没什么问题。看看实际代码会很有用。
  • GameApp.h 是否包含在 Includes.h 中?你的标题有包含保护吗?你有循环包含问题吗?
  • 你确定你的 Circle.h 是一个有效的头文件——你使用它成功地编译了一些东西吗?也许您忘记了类声明末尾的分号?
  • 这至少有助于查看包含Circle 类定义的代码。
  • 请发布 Circle.h 。并在您的任何包含文件中查找不匹配的 }。

标签: c++ class iso


【解决方案1】:

不要不要使用one-massive-include-to-include-everything(预编译的头文件除外)。这几乎肯定会导致头痛。

包括你需要的东西,仅此而已。它会解决你的问题。

您可以将Circle 转发声明为DanDan suggested,但从长远来看,解决您的包含问题将帮助您更多。

【讨论】:

  • 您对 Include.h 中的“定义”或顶点的类型定义有何看法?
【解决方案2】:

您可能有循环包含。使用前向声明:

class GameApp { 
class Cicle;
...

【讨论】:

  • 您刚刚将声明的类 Cicle [原文如此] 作为 GameApp 中的嵌套类转发。然后编译器会去寻找 GameApp::Cicle 类的实现。前向声明应该在 GameApp 的定义之外(和之前)。
【解决方案3】:

我会检查您的 make 文件以确保构建顺序正确,然后在每个 .h 结尾中包含两个 .h 如果两者都很简单,请尝试组合该类。祝你好运

【讨论】:

    【解决方案4】:

    Includes.h 在包含 Circle.h 之前包含 GameApp.h。所以Circle在第一次遇到GameApp的定义时还没有定义。就像 DanDan 说的,向前声明 Circle。

    【讨论】:

      【解决方案5】:

      在 Includes.h 中,您需要将 #include "Circle.h" 移动到 #include "GameApp.h" 之前。更好的是,只需直接从 GameApp.h 包含 Circle.h。每个头文件都应包含直接编译所需的所有内容。

      【讨论】:

        【解决方案6】:

        @James 是对的——Circle.h #includes GameApp.h,如原始编译器消息所示,GameApp.h #includes Circle.h 通过考虑不周的“include-all” Includes.h ,但由于 Circle.h 上有多余的包含保护,所以 #include 没用

        【讨论】:

          猜你喜欢
          • 2011-08-21
          • 1970-01-01
          • 1970-01-01
          • 2015-10-31
          • 2020-12-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多