I suspect this is a very minor issue, but I have spent several hours trying to fix it and have not found anything.
In Game.h (a header file), I have the following:
#pragma once
#include "PlayEngine.h"
class Game {
public:
int Init();
int Run();
int Shutdown();
private:
enum State { ST_MENU, ST_PLAYING } state;
PlayEngine* playengine_;
};
The compiler throws a syntax error on PlayEngine* playengine_, complaining that there is a missing ; before the *. PlayEngine is a class I have defined in other places.
What am I missing?
Replace
#include "PlayEngine.h"
with
class PlayEngine;
A declaration is enough for this case. (and I'm betting the source is a circular inclusion)
Remove state at the end of your enum declaration. and declare the variable separate.
#pragma once
#include "PlayEngine.h"
class Game {
public:
int Init();
int Run();
int Shutdown();
private:
enum State { ST_MENU, ST_PLAYING };
State state;
PlayEngine* playengine_;
};
Related
The IDE I am using is Microsoft Visual Studio 2022 and I am following a guide for game development.
I have the following code:
//Game.h
#pramga once
class Game{
public:
Game();
static int mapWidth;
};
//Game.cpp
#include "Game.h"
//Initialization
int Game::mapWidth;
//Implementation class
Game::Game(){
mapWidth = 10;
};
//Camera.h
#pragma once
#include "Game.h"
class Camera{
Camera() = default;
void update(){
if (Game::mapWidth > 0)
//do something
}
};
What is interesting is that if I hover over this variable in Camera.h the IDE recognizes the variable and where its coming from. But, when i go to compile i get the following error from my if statement:
C2653 'Game': is not a class or namespace name
I am using the scope resolution operator and my vairable is public static and initialized in Game.cpp/h. So, why am I getting this compile error when the IDE recognizes the variable and the Game class as existing?
You seem to be missing a trailing semicolon at the end of your Game class definition. Definitions of structs and classes in C++, unlike in other languages, end in closing bracket AND a semicolon. Such an error might be hard to spot (depending on your IDE). When the preprocessor includes Game.h in Game.cpp, it puts it right above the initialization of Game::mapWidth, which causes it to get invalidated.
Proper definition should like this:
class Game {
public:
static int mapWidth;
Game(); // <-- declaration of constructor should be here
}; // <-- note the semicolon
This is the case for your Camera class as well.
class Camera{
if (Game::mapWidth > 0)
//do something
}; // Semicolon
I'm starting to learn C++ and recently encountered a problem with circular dependency of two headers.
I've already tried forward declaring the Class and namespace, also played around with it in a seperate project but didn't find any solution. Whatever I do the function doesn't get access to the class private members.
Here i simplified the problem a little bit.
A.h
#pragma once
#include "B.h"
class Player {
private:
int m_number;
public:
friend void Byte::getDataChunk(Player& p);
};
B.h
#pragma once
#include <iostream>
#include "A.h"
class Player;
namespace Byte {
void doOtherStuff() {
//other Stuff
}
void getDataChunk(Player& p) {
std::cout << p.m_number;
doOtherStuff();
}
}
I would really like to keep the class and namespace in seperate files, but I don't see any way of doing it. Thanks for your help in advance!
You need to change A.h to include a forward declaration of getDataChunk() in namespace Byte:
#pragma once
#include "B.h"
class Player;
namespace Byte {
void getDataChunk(Player& p);
}
class Player {
private:
int m_number;
public:
friend void Byte::getDataChunk(Player& p);
};
Please note also that including function definitions in header files (i.e. getDataChunk() in B.h) is going to cause you headaches.
Having trouble understanding why I'm getting an 'undeclared identifier' error when I've made sure to include the header file that has the declaration of the class I'm making a vector of.
#pragma once
#include <vector>
#include "Member.h"
class Party {
private:
std::vector<Member> members;
public:
Party();
int get_party_size();
void add_member(Member new_member);
Member& get_member(int num);
};
Here's "Member.h"
#pragma once
#include <vector>
#include <string>
#include "Party.h"
class Member
{
private:
int hp;
bool is_stunned;
bool is_alive;
public:
Member();
~Member();
int get_hp();
bool get_is_stunned();
bool get_is_alive();
void take_damage(int amt);
void stun();
virtual void turn(std::vector<Party>& parties, int my_party, int my_member_number);
virtual std::string get_class_name();
};
Pretty new to the language, so sure I'm missing something obvious.
You have circular dependency between Member and Party
Remove the line
virtual void turn(
std::vector<Party>& parties,
int my_party,
int my_member_number);
in Member and remove the #include "Party.h" in Member.h
Instead think along the lines that a Party is just a collection of Members so there is no need for an individual Member to know about the container
So after input from #some-programmer-dude you could also solve it by adding a forward declaration in your Member.h instead of including the Party.h
class Party;
class Member { ... }
I have decided learn some OOP and I started get rid of conditionals (I know in this program this in unnecessary but I have to start with simply example).
In class DirectConditions, I have my conditions and I would pass object of SnakeGame by reference to friend class DirectConditions's method and change value of some variables of SnakeGame in DirectConditions's method.
Error:
invalid use of incomplite type 'class DirectConditions::SnakeGame' snakeObj.snake[0].xCoor+=1;
Another Question:
May I take this occasion to ask: this way to get rid of conditionals(if's) is good? I would really appreciate help.
Providing the code:
snakeGame.hpp:
#ifndef _SNAKEGAME_HPP
#define _SNAKEGAME_HPP
#include<string>
#include"directConditions.hpp"
#include<map>
class SnakeGame{
public:
SnakeGame();
void mainLogic();
void check();
friend class DirectConditions;
private:
const int width,height;
int sizeOfSnake;
std::string direction;
std::map<std::string,DirectConditions*> directMap;
struct Snake{ int xCoor,yCoor; }snake[100];
enum Directions{up,down,left,right} directEnum;
void initializeMap();
};
SnakeGame::SnakeGame():width(500),height(500),sizeOfSnake(3){}
void SnakeGame::check(){
for(int i=sizeOfSnake;i>0;--i){
snake[i].xCoor=snake[i-1].xCoor;
snake[i].yCoor=snake[i-1].yCoor;
}
directMap[direction].goToDirection(this);
}
//private fun
void SnakeGame::initializeMap(){
directMap["right"]=new GoRight;
directMap["left"]=new GoLeft;
directMap["up"]=new GoUp;
directMap["down"]=new GoDown;
}
#endif
directConditions.hpp:
#ifndef _DIRECTCONDITIONALS_HPP
#define _DIRECTCONDITIONALS_HPP
#include"snakeGame.hpp"
class DirectConditions{
public:
class SnakeGame;
virtual void goToDirection(SnakeGame&)=0;
virtual ~DirectConditions(){};
};
class GoRight:public DirectConditions{
public:
void goToDirection(SnakeGame& snakeObj){
snakeObj.snake[0].xCoor+=1;
}
};
class GoLeft:public DirectConditions{
public:
void goToDirection(SnakeGame& snakeObj){
snakeObj.snake[0].xCoor-=1;
}
};
class GoUp:public DirectConditions{
public:
void goToDirection(SnakeGame& snakeObj){
snakeObj.snake[0].yCoor+=1;
}
};
class GoDown:public DirectConditions{
public:
void goToDirection(SnakeGame& snakeObj){
snakeObj.snake[0].yCoor-=1;
}
};
#endif
You should avoid including of headers by each other - that's why you're getting "invalid use of incomplete type".
Change those includes to forward declarations and move implementation of methods to cpp files.
So, i started learning C++ about a year ago, i learned Java C# and VB.NET before it.
As it is now, i would consider myself a advanced C++ coder. However, theres one thing i dont quite get. The linking process. And heres the problem.
Right now, im coding a XNA-like library for Game development, with a basic Component System -
but i get Compiler errors when building it, C++ pretending it doesnt know a specific type, in this case, the GameComponent class doesnt know the ComponentSelector class (and vice versa), although its correctly included and typed. Im gonna show you my two header files, hopefully you can help me out.
ComponentSelector.hpp:
#ifndef COMPONENTSELECTOR_HPP
#define COMPONENTSELECTOR_HPP
#include<sem/System/Types.hpp>
#include<sem/System/GameComponent.hpp>
#include<vector>
namespace sem
{
class ComponentSelector
{
public:
GameComponent* getComponent1(); //GameComponent does not name a type
GameComponent* getComponent2(); //GameComponent does not name a type
GameComponent* getComponent3(); //GameComponent does not name a type
void addComponent(GameComponent* item); //GameComponent does not name a type
void removeComponent1();
void removeComponent2();
void removeComponent3();
void clearList();
private:
std::vector<GameComponent*> m_Components;
protected:
};
}
#endif // COMPONENTSELECTOR_HPP
GameComponent.hpp:
#ifndef GAMECOMPONENT_HPP
#define GAMECOMPONENT_HPP
#include<sem/System/ComponentSelector.hpp>
#include <sem/System/Types.hpp>
namespace sem
{
class GameComponent
{
public:
virtual void load() = 0;
virtual void unload() = 0;
virtual void update() = 0;
virtual void draw() = 0;
ComponentSelector* m_Selector; //ComponentSelector does not name a type
SEMlong getID();
SEMstring getName();
SEMstring getType();
private:
SEMlong m_ComponentID;
SEMstring m_ComponentName;
SEMstring m_ComponentType;
protected:
};
}
#endif // GAMECOMPONENT_HPP
Any solution and tips would be greatly appreciated.
You need a forward declaration for class ComponentSelector in your GameComponent class declaration:
// Remove this: #include<sem/System/ComponentSelector.hpp>
#include <sem/System/Types.hpp>
namespace sem
{
class ComponentSelector; // Note the forward declaration added!
class GameComponent
{
// ...
ComponentSelector* m_Selector; // Compiles now!
// ...
I'd recommend to do the same in the ComponentSelector declaration header vice versa. Include the full class declarations then, where you are going to use any members (this is in the compilation units i.e. .cpp-files usually).