gpt4 book ai didi

c++ - 声明从具有虚函数的基类派生的对象时出现错误 c2143 和 c4430

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:33 25 4
gpt4 key购买 nike

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

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

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

title.h(35): error C2143: syntax error : missing ';' before '*'

title.h(35): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

我花了几个小时试图自己解决问题。我在 google 上搜索过,也一直在搜索 stackoverflow,但是虽然我看到并阅读了许多其他有类似问题的帖子,但似乎没有一个与我有同样的问题。

基于其他人发布的其他类似问题以及他们接受的答案,我已经搜索过:#include 循环问题,确保类声明具有最终的 ;,并确保包含所有 #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

最佳答案

编译器似乎不知道MenuButton 是什么。出现这种情况的一个原因是 MENUBUTTON_H 也由另一个 header 定义。更有可能的是,类似 Globals.h 的内容也包含 "title.h",即您的系统中有一个依赖循环。如果你知道类是循环依赖的,你最好去掉循环。有很多方法可以做到这一点。即使您当前认为您需要循环依赖,它也不是很好而且可能不需要(John Lakos 的“Large Scale C++ Design”一书包含了很多关于如何打破依赖循环的信息)。

如果您不知道循环从何而来,我建议您在编译器中使用 -E(或 /E)标志:此选项通常会产生预处理后的翻译单元。它将包含以某种形式输入的文件的指示。预处理输出的外观取决于编译器,但您将能够看到编译器看到的内容,并且您可能可以找到未声明 MenuButton 的原因。

作为一种简单的方法,您还可以确保声明了 MenuButton:要形成一个指针,您不需要它的定义。在实际访问指针时看到定义就足够了。声明看起来像这样:

class MenuButton;

.. 并且可以在每个翻译单元中重复使用 MenuButton 来声明 MenuButton 的指针或引用成员。您甚至可以仅通过声明声明按值返回或传递 MenuButton 的函数。当然,当你想实际使用该类型时,你需要查看它的定义。

关于c++ - 声明从具有虚函数的基类派生的对象时出现错误 c2143 和 c4430,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13906724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com