How can it be that the following code generates a C2504: 'GameObject': base class undefined?! error:
#ifndef INCLUDED_PLAYER
#define INCLUDED_PLAYER
#include "GameObject.h"
#include "Game.h"
#include "Bullet.h"
#include <SFML/Window/Keyboard.hpp>
class GameObject;
class Player:
public GameObject
{ <- Compiler Error
Edit: Problem found, for whatever reason I added an unnecessary include in GameObject.h. Removing that include fixed all my problems.
Related
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 2 months ago.
My code works normally up until the point that I add #include "CharacterBase.h" to a file called ItemBase.h. I am using #pragma once in all of my files, and I cannot figure out why when I add CharacterBase.h it suddenly is filled with errors. "Itembase" undeclared identifier, is the error.
//Filename is :ItemBase
#pragma once
#include <string>
#include "CharacterBase.h"
class ItemBase
{
}
//Filename is ItemConsumable
#pragma once
#include "ItemBase.h"
class ItemConsumable : public ItemBase
{
}
//File name is CharacterBase.h
#pragma once
#include <string>
#include "ItemBase.h"
#include "ItemConsumable.h"
#include <vector>
class CharacterBase
{
public:
}
You just have a circular dependencie.
In C++, if your file “CharacterBase.h” includes “ItemBase.h” then “ItemBase.h” cannot include “CharacterBase.h”. The only way for ItemBase to use CharacterBase is to forward declare CharacterBase, use pointers or references on CharacterBase in the header and finally include “CharacterBase.h” in “ItemBase.cpp”.
File ItemBase.h
//ItemBase.h
#pragma once
#include <string>
//ItemBase.h
#include "CharacterBase.h"
class ItemBase
{
}
File CharacterBase.h
//CharacterBase.h
#pragma once
#include "ItemBase.h"
class ItemBase{} // Forward declaration
class CharacterBase
{
public:
CharacterBase(ItemBase *base);
ItemBase *itemBase;
}
File CharacterBase.cpp
// CharacterBase.cpp
#include ItemBase.h
CharacterBase:CharacterBase(ItemBase *itemBase){// your stuff}
I'm trying to add inheritance to my objects based on SFML and I can't solve a problem with errors
*expected class-name before '{' token*
and when I try to solve it by pre-declaring my parent class I'm getting stopped by
*invalid use of incomplete type 'class Object'*
I made Rectangle and Circle class with Object as parent class
Object.hpp
#ifndef OBJECT_HPP_INCLUDED
#define OBJECT_HPP_INCLUDED
#include "../MainClass.hpp"
#include "../Data/Data.hpp"
#include <SFML/Graphics.hpp>
#include <iostream>
class Object
{
public:
Object();
virtual ~Object();
virtual void update() = 0;
virtual void draw() = 0;
};
#endif // OBJECTPARENT_HPP_INCLUDED
in Object.cpp file I only have definitions of constructor and deconstructor
Rectangle.hpp
#ifndef RECTANGLE_HPP_INCLUDED
#define RECTANGLE_HPP_INCLUDED
#include "Object.hpp"
#include <SFML/Graphics.hpp>
#include "../Data/Data.hpp"
#include "../MainClass.hpp"
#include <iostream>
#include <cstdlib>
class Object; // <--- without this line *expected class-name before '{' token*, but with it *invalid use of incomplete type 'class Object'*
class Rectangle : public Object
{
public:
Rectangle(/*args*/);
virtual ~Rectangle();
//some functions
void update();
void draw();
private:
//some members
};
#endif // RECTANGLE_HPP_INCLUDED
and Circle.hpp
#ifndef CIRCLE_HPP_INCLUDED
#define CIRCLE_HPP_INCLUDED
#include "Object.hpp"
#include <SFML/Graphics.hpp>
#include "../Data/Data.hpp"
#include "../MainClass.hpp"
#include <iostream>
#include <cstdlib>
class Object; // <--- without this line *expected class-name before '{' token*, but with it *invalid use of incomplete type 'class Object'*
class Circle: public Object
{
public:
Circle(/*args*/);
virtual ~Circle();
//some functions
void update();
void draw();
private:
//some members
};
#endif // CIRCLE_HPP_INCLUDED
I don't know what to do, I tried changing the order of all includes, removing stuff and adding it, i searched everywhere but there was no good solution. Thank you for all answers
just insert ' #include "Object.hpp" ' in Circle.hpp and Rectangle.hpp and any other file like them that is using Object class:
#include "ObjectParent.hpp"
#include <SFML/Graphics.hpp>
#include "../Data/Data.hpp"
#include "../MainClass.hpp"
#include "Object.hpp" // <----------
#include <iostream>
#include <cstdlib>
My previous question was asked wrong, so I'll post it fixed.
I have this example throwing
expected class-name before ‘{’ token
error while compiling. I am understanding why is it fails, but I don't know how to fix it. Thank you.
BaseClass.h
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h"
class ElementClass;
class BaseClass
{
private:
ElementClass *m_someField;
};
#endif
ElementClass.h
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h"
class ChildClass;
class ElementClass
{
private:
ChildClass *m_class;
};
#endif
ChildClass.h
#ifndef INHERITTEST_CHILDCLASS_H
#define INHERITTEST_CHILDCLASS_H
#include "BaseClass.h"
class ChildClass : public BaseClass
{
};
#endif
You have circulary dependent .h files.
In BaseClass.h:
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h" // Includes ElementClass.h
In ElementClass.h:
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h" // Which included BaseClass.h
You can remove those #include lines since you are using those classes by pointers only and a forward declaration is sufficient for that purpose.
When you're working with inheritance the following
#include "ChildClass.h"
class ChildClass;
is unnecessary, if you're going to break these into sepperate source files (which it looks like you are) you can say
#include "ElementClass.h"
in the source file of your derived class
I have this code in C++:
#ifndef MYCLASS_H
#define MYCLASS_H
#include "gspace.h"
class myclass {
public:
void update(gspace **);
}
gspace is another class defined in gspace.h. The compiler however is telling me:
include/myclass.h error: ‘gspace’ has not been declared|
Is there anything wrong in what I'm doing?
EDIT:
#ifndef GSPACE_H
#define GSPACE_H
#include <vector>
#include <iostream>
#include <math.h>
using namespace std;
class gspace
{
public:
gspace();
Assuming you have even declared a class named "gspace" in gspace.h I could think of two possible errors.
1) class gspace would not be in global scope i.e could be in some namespace.
2) Please check if header gaurds used in gspace not "MYCLASS_H".
So I am getting the following errors:
..\Actor.h:35: error: `Attack' is not a member of `RadiantFlux'
..\Actor.h:35: error: template argument 1 is invalid
..\Actor.h:35: error: template argument 2 is invalid
..\Actor.h:35: error: ISO C++ forbids declaration of `attacks' with no type
On this line (among others):
std::vector<RadiantFlux::Attack> attacks;
Here are the relevant files:
Actor.h:
#ifndef ACTOR_H_
#define ACTOR_H_
#include <string>
#include <vector>
#include "Attack.h"
namespace RadiantFlux {
...
class Actor {
private:
std::string name;
int health;
std::vector<RadiantFlux::Attack> attacks;
Attributes attributes;
public:
...
};
}
#endif /* ACTOR_H_ */
Attack.h:
#ifndef ATTACK_H_
#define ATTACK_H_
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Actor.h"
namespace RadiantFlux {
...
class Attack {
private:
...
public:
...
};
}
#endif /* ATTACK_H_ */
Why am I getting these errors and what can I do to fix them? I am assuming it has something to do with the namespaces...
You have a cyclic dependency of your header files.
Attack.h includes Actor.h and vice versa.
Use Forward Declaration of class to avoid circular dependency problems.
Since the OP's comments, here is what needs to be done:
class Actor;
class Attack
{
};
If your code fails to compile after doing this, You need to read the linked answer and Understand why the error and how to solve it. The linked answer explains it all.
The classes Actor and Attack both refer to each other, so you will need to add a forward declaration in one of the file.
For example, in Actor.h:
class Attack;
class Actor
{
...
};