#Include causing undeclared identifier error [duplicate] - c++

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}

Related

Class Name does not declare type C++

I am trying to compile my code, but I am getting an error with classes. One of the classes compiled fine(Example.h), but the other(Name.h) keeps giving me the class does not name type error. I think it has something to do with circular dependency, but how do I fix that without a forward deceleration?
Name.h
#ifndef _NAME
#define _NAME
#include <iostream>
#include <string>
using namespace std;
#include "Example.h"
class Name{
};
#endif
Example.h
#ifndef _EXAMPLE
#define _EXAMPLE
#include <iostream>
#include <string>
using namespace std;
#include "Name.h"
class Example{
};
#endif
I saw a post about using forward deceleration, but I need to access memebers from the Example class..
You have a circular dependency, where each header tries to include the other, which is impossible. The result is that one definition ends up before the other, and the name of the second is not available within the first.
Where possible, declare each class rather than including the entire header:
class Example; // not #include "Example.h"
You won't be able to do this if one class actually contains (or inherits from) another; but this will allow the name to be used in many declarations. Since it's impossible for both classes to contain the other, you will be able to do this (or maybe just remove the #include altogether) for at least one of them, which should break the circular dependency and fix the problem.
Also, don't use reserved names like _NAME, and don't pollute the global namespace with using namespace std;
see, here you are including #include "Example.h" in Name.h and #include "Name.h" in Example.h. suppose compiler compiles Name.h file first so _NAME is defined now, then it tries to compile Example.h here compiler wants to include Name.h but the content of Name.h will not be included in Example.h since _NAME is already defined, hence class Name is not defined inside Example.h.
you can explicitly do forward declaration of class Name; inside Example.h
Try this:
Name.h
#ifndef NAMEH
#define NAMEH
#include <iostream>
#include <string>
using namespace std;
//#include "Example.h"
class Example;
class Name{
};
#endif
Name.cpp
#include "Name.h"
#include "Example.h"
...
Example.h
#ifndef EXAMPLEH
#define EXAMPLEH
#include <iostream>
#include <string>
using namespace std;
//#include "Name.h"
class Name;
class Example{
};
#endif
Example.cpp
#include "Example.h"
#include "Name.h"
...

C++ undefined reference to vtable on compile [duplicate]

This question already has answers here:
Undefined reference to vtable
(21 answers)
Closed 9 years ago.
Why do I keep getting an error on compile with this code?
#ifndef OPERATOR_H
#define OPERATOR_H
#include <cstdlib>
#include <iostream>
#include <string>
#include "Individual.h"
using namespace std;
class Operator
{
public:
Operator();
virtual void execute (Individual* parent);
private:
};
#endif
Then in the cpp file I have
#include <cstdlib>
#include <iostream>
#include <string>
#include "Operator.h"
using namespace std;
Operator::Operator()
{
}
void execute(Individual* parent)
{
}
Define a destructor. The vtable for a class with virtual methods is created in the translation unit that defines the constructor.
In your header, add:
virtual ~Operator();
And in your source file, add:
Operator::~Operator() {
}

Undeclared Identifier vector of pointers to objects

Error: Line 12 of Cell.h: 'Actor' undeclared identifier.
If I try to forward declare above it, it says that there's a redefinition. What do I do?
Actor.h:
#ifndef ACTOR_H
#define ACTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Cell.h"
using namespace std;
class Actor //Simple class as a test dummy.
{
public:
Actor();
~Actor();
};
#endif
Cell.h:
#include <iostream>
#include <string>
#include <vector>
#include "Actor.h"
#ifndef CELL_H
#define CELL_H
using namespace std;
class Cell // Object to hold Actors.
{
private:
vector <Actor*> test;
public:
Cell();
~Cell();
vector <Actor*> getTest();
void setTest(Actor*);
};
#endif
Cell.cpp:
#include "Cell.h"
#include <vector>
vector<Actor*> Cell::getTest() //These functions also at one point stated that
{ // they were incompatible with the prototype, even
} // when they matched perfectly.
void Cell::setTest(Actor*)
{
}
What else can I do?
Remove the #include "Cell.h" from Actor.h and you're set to go.
In general, prefer forward declarations where you can, and includes where you must. I'd also replace the #include "Actor.h" from Cell.h with a forward declaration: class Actor;.
In the cpp files you can include the headers if you need them.
You have recursive #includes via your mutual references between cell.h and actor.h.
In Cell.h, delete #include <Actor.h>.
In Cell.h, add the line class Actor; just above the definition of class Cell.
In Cell.cpp, you might need to add #include "Actor.h".

'C2504 base class undefined' error despite #include and forward declaration?

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.

Namespace problems

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
{
...
};