Two errors to do with using a class - c++

I'm getting these two errors:
sdlsetup.h(15): error C2143: syntax error : missing ';' before '*'
sdlsetup.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
This is sdlsetup.h line 15:
CText* ctext;
The class is definitely set up properly and included in sdlsetup.h
Why is this?
EDIT: This is sdlsetup.h
#pragma once
#include "stdafx.h"
class SDLsetup
{
public:
SDLsetup();
~SDLsetup();
void Begin();
void End();
SDL_Renderer* GetRenderer();
void EnterGameLoop();
CText* ctext;
private:
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event* mainEvent;
int screenWidth;
int screenHeight;
};
This is Text.h
#pragma once
#include "stdafx.h"
class CText
{
public:
CText();
~CText();
void draw(SDL_Renderer* renderer, std::string str);
SDL_Texture* aGetMessage();
SDL_Rect* aGetMessageRect();
private:
TTF_Font* Sans;
SDL_Color White;
SDL_Surface* surfaceMessage;
SDL_Texture* Message;
SDL_Rect Message_rect;
};

Here the problem is a weird error message from the compiler, because it doesn't know what CText is.
You need to declare CText before you use it:
#pragma once
// Declare the CText class, so the compiler knows it exists
class CText;
class SDLsetup
{
public:
...
CText* ctext;
...
};

You need to add includes for Text.h in sdlsetup.h and also for the relevant SDL headers in both files.
Text.h needs to have SDL.hand SDL_ttf.hincluded and sdlsetup.h needs SDL.hand Text.h

Related

Unknown override specifer

I'm trying to create a game using C++ and SDL (I'm still new and learning).
I've been stuck on this for some time, and I'm not sure how to solve this.
This is Game.h where I'm trying to create the actual game:
#pragma once
#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
#include "TextureManager.h"
#include "GameObject.h"
#include "Input.h"
struct Objects
{
GameObject player;
Input input;
};
class Game
{
public:
Game() {};
~Game() {};
void init(int width, int height, bool fullscreen);
void handleEvents();
void update();
void render();
void clean();
bool inline running()
{
return _isRunning;
}
static SDL_Renderer* renderer;
static SDL_Event event;
private:
bool _isRunning = true;
SDL_Window* _window = nullptr;
SDL_Texture* texture = nullptr;
GameObjectsRef data = std::make_shared<Objects>();
};
And this is GameObjects.h where I'm doing things related to objects of the game:
#pragma once
#include <string>
#include "TextureManager.h"
class GameObject
{
public:
GameObject() {};
~GameObject() {};
void init();
void render();
void update();
int xpos = 0;
int ypos = 0;
int dx = 0;
int dy = 0;
private:
SDL_Texture* texture = nullptr;
//GameObjectsRef data;
};
I found some answers where it says there might be issues with multiple includes, but shouldnt #pragma once take care of that?
Error messages in the Game.h lines 12 and 13
Error C3646 'player': unknown override specifier
Error C4430 missing type specifier - int assumed. Note: C++ does not support
Error C3646 'input': unknown override specifier
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int

missing type specifier - int assumed Error after declaration of static member

I get the following error message after compiling my code:
Error C4430 missing type specifier - int assumed. Note: C++ does not
support default-int on line 21
I dont really know why the AssetManager retrieves that error, since I included everything needed...
My code:
game.hpp
#pragma once
#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
#include <vector>
#include "AssetManager.hpp"
class Game {
public:
Game();
~Game();
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
void handleEvents();
void update();
bool running() { return isRunning; };
void render();
void clean();
static AssetManager* assets;
////////
}
AssetManager.hpp
#pragma once
#include <map>
#include <string>
#include "TextureManager.hpp"
#include "Vector2D.hpp"
#include "ECS.hpp"
class AssetManager {
public:
AssetManager(Manager* man);
~AssetManager();
void AddTexture(std::string id, const char* path);
SDL_Texture* GetTexture(std::string id);
private:
Manager* manager;
std::map<std::string, SDL_Texture*> textures;
};
I think this is a problem of circular dependency. Try forward declaration for AssetManager something like this:-
Class AssetManager; instead of #include "AssetManager.hpp" in game.hpp

Using a header file in one code file and then vice versa?

So I've got a bit of a problem (well two but they're unrelated to each other).
I have two headers that look as follows:
Game.h
#ifndef INIT_GAME_H
#define INIT_GAME_H
#include <deps/deps.h>
#include <handlers/RenderHandler.h>
class Game {
private:
RenderHandler* renderHandler; /* <-- This is line 32 in my actual header */
public:
Game() {};
~Game() {};
int initialise();
void handleEvents();
void update();
void render();
void clean();
}; // class Game
#endif // INIT_GAME_H
RenderHandler.h
#ifndef HANDLERS_RENDERHANDLER_H
#define HANDLERS_RENDERHANDLER_H
#include <init/Game.h>
class RenderHandler {
private:
Game* game;
public:
RenderHandler() {};
~RenderHandler() {};
void initialise(Game* game);
void render();
}; // class RenderHandler
#endif // HANDLERS_RENDERHANDLER_H
But the above gives me an error during compilation:
game.h(32): error C2143: syntax error: missing ';' before '*'
game.h(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
game.h(32): error C2238: unexpected token(s) preceding ';'
As you've probably guessed, I'm trying to store Game's instance in RenderHandler and vice versa. I'm probably doing it completely the wrong way but I can't figure why it's not working.
Also, all ; are in their right places prior to line 32 in my header file.
EDIT:
after doing the suggested forward declaration, I get the following error (now in RenderHandler.cpp file).
Error: pointer to incomplete class type is not allowed
This is what my code file looks like
RenderHandler.cpp
#include <handlers/RenderHandler.cpp>
void RenderHandler::initialise(Game* game) {
this->game = game;
}
void RenderHandler::render() {
glfwSwapBuffers(game->getPrimaryWindow());
}
Use forward declaration :
#ifndef HANDLERS_RENDERHANDLER_H
#define HANDLERS_RENDERHANDLER_H
// FW declaration of Game
class Game;
class RenderHandler {
private:
Game* game;
public:
RenderHandler() {};
~RenderHandler() {};
void initialise(Game* game);
void render();
}; // class RenderHandler
#endif // HANDLERS_RENDERHANDLER_H

C++: circular dependency issue

I'm having a problem with circular dependencies, I suppose this is a design flaw from introducing the Game class in the wrong way.
Game.h:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameScreen.h"
#include "TitleScreen.h"
class Game
{
protected:
sf::RenderWindow window;
GameScreen* CurrentGameScreen;
TitleScreen Title;
public:
Game(void);
~Game(void);
sf::RenderWindow getWindow(void);
void Run();
void Close();
};
GameScreen.h:
#pragma once
#include "Game.h"
class GameScreen
{
public:
GameScreen(void);
~GameScreen(void);
virtual void LoadAllResources() {};
virtual void DrawScreen(Game* game) {};
virtual void Update(Game* game) {};
};
TitleScreen.h:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameScreen.h"
class TitleScreen : public virtual GameScreen
{
private:
sf::Texture title_screen;
sf::Sprite titleScreen;
sf::Font font;
sf::Text menuExit;
public:
TitleScreen(void);
~TitleScreen(void);
void LoadAllResources();
void DrawScreen(Game* game);
void Update(Game* game);
};
Then there's the main file:
#include "Game.h"
int main()
{
Game game;
game.Run();
//sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
//GameScreen* currentScreen;
//TitleScreen titleScreen;
//currentScreen = &titleScreen;
//while (window.isOpen())
//{
// currentScreen->Update(&window);
// currentScreen->DrawScreen(&window);
//}
return 0;
}
GameScreen.h and TitleScreen.h raise a handful of C2061. From what I understand these are caused by circular dependencies between Game.h and Gamescreen.h.
TitleScreen.h is giving me error C2504: 'GameScreen' : base class undefined.
Game.h: on lines 12 and 13, give C2143: syntax error : missing ';' before '*', although I'm not sure where this is coming from and my IDE is not giving me any syntax errors.
If I remove the #include statement from GameScreen.h and substitute it with a forward declaration class Game; (which I guess breaks the circular dependency?) most of the above is solved, but TitleScreen.cpp throws a set of C2027, C2227 and C2228 (undefined type, left of -> and left of .) every time I try to access a Game object. IntelliSense points out that a pointer to an incomplete class is not allowed.
I got it working before introducing the Game class - DrawScreen() and Update() would take as argument a pointer to window (sf::RenderWindow* window). There's part of the old code left in main.cpp.
In the GameScreen.h you should declare the Game class instead of including its whole header file, so this:
class Game;
instead of:
#include "Game.h"

Error with class causing a few hundred errors

My header for my class is
#ifndef _CENGINE_H
#define _CENGINE_H
#include "SFML\Graphics.hpp"
#include "CTextureManager.h"
#include "CTile.h"
class CEngine
{
private:
//Create instance of CTextureManager
CTextureManager textureManager;
//Load textures
void LoadTextures();
//New tile
CTile* testTile;
bool Running; //Is running?
sf::RenderWindow* window; //Create render window
public:
CEngine(); //Constructor
int Execute(); //Execute
bool OnInit(); //On intialization
void GameLoop(); //Main game loop
void Render(); //Render function
void Update(); //Update
};
#endif
Now the 3 errors it's giving me with this are:
cengine.h(8): error C2236: unexpected 'class' 'CEngine'. Did you forget a ';'?
cengine.h(8): error C2143: syntax error : missing ';' before '{'
cengine.h(8): error C2447: '{' : missing function header (old-style formal list?)
I know the errors are obvious but I can't see a problem with the class. I'm probably being really stupid because I'm tired.
It seems like a circular include issue. Do CTextureManager.h or CTile.h include each other or CEngine.h?
To solve this, use forward declarations where possible. For example, your class doesn't need to include CTile.h - it can look like:
#ifndef CENGINE_H
#define CENGINE_H
#include "SFML\Graphics.hpp"
#include "CTextureManager.h"
class CTile; //forward declaration instead of include
class CEngine
{
private:
//Create instance of CTextureManager
CTextureManager textureManager;
//Load textures
void LoadTextures();
//New tile
CTile* testTile;
bool Running; //Is running?
sf::RenderWindow* window; //Create render window
public:
CEngine(); //Constructor
int Execute(); //Execute
bool OnInit(); //On intialization
void GameLoop(); //Main game loop
void Render(); //Render function
void Update(); //Update
};
#endif
Similar for the other 2 headers.
Also, _CENGINE_H is not a valid identifier - note how I renamed it to CENGINE_H.