1

这是我第一次在 stackoverflow 上发帖,所以希望这是遵守所有礼仪!

我正在尝试使用 SDL 1.x 扩展库在 C++ 中创建和实现一个按钮类,以便与使用有限状态机的个人项目一起使用。我有一个带有虚函数的基类,“Button”,一个从 Button 派生的类,“MenuButton”,以及一个试图创建 MenuButtons 的“Title”状态。

我正在使用 Microsoft Visual Studio 2012,Intellisense 说我的代码是正确的。但是,当我尝试编译时,我收到以下两个错误:

title.h(35):错误 C2143:语法错误:缺少 ';' 前 '*'

title.h(35):错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持默认整数

我花了几个小时试图自己找出问题所在。我搜索了谷歌,也一直在搜索 stackoverflow,但是虽然我已经看到并阅读了许多其他类似问题的帖子,但似乎没有一个与我有同样的问题。

根据其他人发布的其他类似问题以及他们接受的答案,我已经搜索过:#include 循环问题,确保类声明有一个 final ;,并确保包含所有#includes(我知道) .

代码片段如下,如果有人能告诉我如何解决这个问题,我将不胜感激。

谢谢你

Button.h(基类)

#ifndef BUTTON_H
#define BUTTON_H

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "Functions.h"
#include "Globals.h"
#include "Constants.h"
#include <string>

using namespace std;

class Button
{
public:
    virtual void handleEvents() = 0;

    virtual void render() = 0;

    //virtual ~Button(void){};
protected:
    //Our enumerated type
    const enum mouseStates {CLIP_MOUSEOUT, CLIP_MOUSEOVER, CLIP_MOUSEDOWN, CLIP_MOUSEUP};

    //Our rect to hold the offsets and size of the button
    SDL_Rect button;

    //Array of rects to hold the dimensions of each clip from the sprit sheet
    SDL_Rect clips[4];

    //Holds the location and size of the clip to show from sprite sheet
    SDL_Rect *clip;

    //The sprite sheet surface from which we clip the button images
    SDL_Surface *buttonSpriteSheet;

    //The surface for our button text
    SDL_Surface *buttonSurfaceText;

    //The text for our button
    string buttonText;

    //The state the button will lead to when clicked
    GameStates targetState;

    //Whether we are moused over the button
    bool mouseOver;
};

#endif

MenuButton.h(派生类)

#ifndef MENUBUTTON_H
#define MENUBUTTON_H

#include "button.h"

using namespace std;

class MenuButton :
    public Button
{
public:
    MenuButton(void);

    MenuButton(int x, int y, int w, int h, string text, GameStates state);

    void handleEvents();

    void render();

    ~MenuButton(void);
};

#endif

Title.h(创建按钮并抛出错误的状态)

#ifndef TITLE_H
#define TITLE_H

#include "GameState.h"
#include "SDL.h"
#include "SDL_image.h"
//#include "SDL_Mixer.h"
#include "Functions.h"
#include "Globals.h"
#include "Constants.h"
#include "MenuButton.h"
#include <iostream>
#include <string>

using namespace std;

class Title :
    public GameState
{
public:
    Title(void);

    //Handles intro events
    void handleEvents();

    //Handles intro logic
    void logic();

    //Handles intro rendering
    void render();

    ~Title(void);
private:
    SDL_Surface *background;
    MenuButton *testButton; //NOTE: This is the line throwing the errors
    //Mix_Music *music;
};

#endif

感谢您的帮助!~<3

4

1 回答 1

1

It seems the compiler doesn't know what MenuButton is. One reason this may be the case is that MENUBUTTON_H is also defined by another header. More likely, something like Globals.h also included "title.h", i.e., you have a dependency loop in your system. If you know about classes being cyclically dependent, you are best off removing the cycles. There are many ways to do that. Even if you currently think you need the cyclic dependency, it is not good and probably not required (the book "Large Scale C++ Design" by John Lakos contains a lot of information on how to break dependency cycles).

If you don't know where the cycle is coming from, I recommend using the -E (or /E) flag with your compiler: This option normally yields the translation unit after it got preprocessed. It will contain indications on which file was entered in some form. How the preprocessed output looks like depends on the compiler but you'll be able to see what the compiler gets to see and you can probably track down why MenuButton isn't declared.

As an easy way out you can also make sure that MenuButton is declared: To form a pointer, you don't need its definition. It is sufficient that you have seen a definition by the time you actually access the pointer. A declaration would look something like this:

class MenuButton;

.. and can be repeated in each translation unit using MenuButton to declare pointer or reference members for MenuButton. You can even declare functions returning or passing MenuButton by value with just a declaration. Of course, when you want to actually use the type, you'd need to see its definition.

于 2012-12-17T00:24:48.860 回答