This is my first time posting on stackoverflow, so hopefully this is observing all etiquette!
I am attempting to create and implement a button class in C++ using the SDL 1.x extension library for use with a personal project using a Finite State Machine. I have a base class with virtual functions, "Button", a class derived from Button, "MenuButton", and a "Title" state that attempts to create MenuButtons.
I’m using Microsoft Visual Studio 2012, and Intellisense says that my code is correct. When I attempt to compile however I receive the following two errors:
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
I’ve spent several hours trying to figure out the problem on my own. I’ve searched google, and have been searching stackoverflow as well, but while I have seen and read numerous other posts with similar problems, none of them seemed to have the same issue I am.
Based on other similar problems posted by others, and their accepted answers, I’ve already searched for: #include loop problems, making sure class declarations have a final ;, and making sure that all #includes are included (that I know of).
The code snippets are below, I would be grateful for the wisdom and assistance if anyone would be able to tell me how to solve this.
Thank You
Button.h (the base class)
#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 (Derived class)
#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 (The state which is creating a button and throwing the error)
#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
Thank you for the assistance!~<3
It seems the compiler doesn’t know what
MenuButtonis. One reason this may be the case is thatMENUBUTTON_His also defined by another header. More likely, something likeGlobals.halso 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 whyMenuButtonisn’t declared.As an easy way out you can also make sure that
MenuButtonis 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:.. and can be repeated in each translation unit using
MenuButtonto declare pointer or reference members forMenuButton. You can even declare functions returning or passingMenuButtonby value with just a declaration. Of course, when you want to actually use the type, you’d need to see its definition.