I have 6 C++ header files. There are a lot of includes so I tried to make it so that I use as little as I can. But I keep getting an error from the very beginning saying that a class "Agent" is undefined. I defined it and included it and can't find the problem here are the 2 header files that are causing the problem:
Sinbad.h:
#ifndef SINBAD_H
#define SINBAD_H
#pragma once
#include "Agent.h"
#define NUM_ANIMS 13 // number of animations the character has. Should be made character specific
class Agent;
class Sinbad : public Agent {
Agent.h:
#ifndef AGENT_H
#define AGENT_H
#include <deque>
#include "aStar.h"
extern Ogre::SceneManager* sceneMgr; // Defined in main.cpp
class GridNode; // forward declarations
class Grid;
class Astar;
class Agent {
Here's the error I am getting:
1>c\gameengine_solution\sinbad.h(12) : error C2504: 'Agent' : base class undefined
It looks like something is referring to class Agent before it is defined, i.e. probably in aStar.h.
EDIT: Search for #define AGENT_H everywhere in your source. If you find it anywhere outside Agent.h, that means the Agent class might never be defined, if only because Agent.h can be #included with AGENT_H already #defined.
Do not redeclare class Agent in Sinbad.h. You already included Agent.h. Seems also to be the case in Agent.h with Grid and Astar.
Some of the comments suggest you're unsure how forward declaring works.
Forward declare a type when you need to tell the compiler that the type exists, but do not need any of its definition. Generally, this is done in a header when a class's members or methods only have pointers or references to the type. In order to do anything that involves dereferencing the pointer or using the reference, you will need to include the header that defines the type, usually in the source that defines the methods of the class.
Related
Okay So I'm making my own Entity Component System, and I'm trying to have an Entity pointer object within a Component class, but the object(variable) gives me a ton of errors, even though it has been included.
'Component.h'
#include "Entity.h"
class Component {
public:
Entity* ThisEntity;
}
That doesn't work, and gives me 76 errors inside 'Entity.h', all of which don't recognize my custom types(like Component, string, etc).
I usually use a global header file which has all the global variables, and includes everything necessary, like this:
'Public.h'
#ifndef Entity_h
#include "Entity.h" // Entity is include guarded
#endif
When i try to include 'Public.h' inside 'Component.cpp' it still gives me errors:
'Component.cpp'
#include "Public.h"
#include "Component.h" // The arrangement is correct, public before component but this still doesn't work
When I hover over the pointer variable "Entity* ThisEnt" it shows me "Entity Class", so it recognizes it but it still gives me 76 errors.
You can not refer to header files from each other as it creates a cyclical dependency.
You can break that dependency by forward declaring one of the classes you want to use.
Forward declaring the class 'Entity' before the class 'Component' seemed to fix the issue.
I have a rather simple problem
This is my firstcluster.h
#pragma once
#include "cluster.h"
class FirstCluster:public Cluster{
...
public:
...
};
Code for cluster.h:
#pragma once
// File: cluster.h
class Cluster {
protected:
...
public:
...
};
And i'm getting the error:
error C2504: 'Cluster' : base class undefined
Sometimes i get this IntelliSense error:
IntelliSense: incomplete type is not allowed ... Line 10 Column 27
But it doesn't always come up.
The cluster.h is included, as you can see, and all other header files are protected with #pragma once
I really don't know what could go wrong here?
Can circular include make problems even if i protected everything with #pragma once?
I'm using Visual Studio 2010.
I got this due to circular include.
I included all my headers in "include.h" and include it in everywhere else.
I managed to avoid circular include by just including standard headers in include.h.
I've had the exact same problem, adding
#ifndef CLUSTER_H
#define CLUSTER_H
/* your code */
#endif
helped to solve the problem. the ifndef part is obviously for include duplications but "define", i think, did help.
This code looks normal, so:
if cluster.h actually defines Cluster class, then check for missing namespace around Cluster (if you use a namespace), upper-lower case usage in 'Cluster' vs 'cluster', 'cLuster', etc., also check that the Cluster definition is not local to another class.
Hope this helps.
This error occurs due to circular include so We can handle this by adding the header of child class in the bottom of the parent class
I have the class below which inherits from a Collection class where I have virtual functions defined that I need to later on implement in my derived class below. I have yet to include the definitions for my member functions, except for the constructor of my derived class, in the .cpp file. However when I build my project, I get the following error message
expected class-name before '{' token|
I have tried everything I know to try, and am in need of assistance in understanding what I have wrong in my code and how I can go about fixing it.
#ifndef VARIABLEARRAY_H
#define VARIABLEARRAY_H
#include "Collection.h"
using namespace std;
class VariableArray: public Collection{
int* list[];// dynamic array that is resized on demand
public:
VariableArray();
};
#endif
any help will be greatly appreciated.
Are you sure the Collection symbol has already been seen by your translation unit?
You may want to add:
#include "Collection.h"
(or whatever the correct name is) before the class definition.
I think you could also forward declare Collection:
class Collection;
class VariableArray : public Collection {
....
};
How did you determine if Collection is available to the compiler? Does the class declaration for Collection show up in the preprocessed output of your VariableArray.cpp file, or whatever the corresponding source file is?
Looking at the preprocessed output can help you determine:
if namespace pollution is an issue (e.g. #define collides with Collection)
if the Collection declaration is truly available before your VariableArray declaration.
If it's not a namespace pollution issue, I'd check if you are using the same #include guards in more than one header file.
I have a C++ project in Visual Studio 2008.
In the project I have several forms and several non-form classes. One non-form specifically called Import_LP.h that is a class with several methods all of which are written in the header file with nothing in the resource file.
I have no problem with #include Import_LP in any of the form classes and creating objects and referencing any of its methods, however any other class I try to #include it into, it gives me a
syntax error : undeclared identifier 'Import_LP'
on the line it is referenced occurs ie Import_LP^ importLP;
I come from a java/c# background is there something I'm missing with the linking here?
If you have include guards, it goes like this: the preprocessor includes Import_LP.h, which says "only include me once", then includes Window.h, which tries to include Import_LP.h, but doesn't because of the include guard. So Window.h starts parsing the window class, but fails because the Import_LP.h class header hasn't fully loaded yet.
The solution is to predeclare the classes:
Window.h:
#ifndef WINDOW_H //works best if this is first
#define WINDOW_H
#pramga once
class Import_LP;
class Window {
Import_LP* member; //member has to be a pointer
void func();
};
#include "Import_LP.h"
inline void Window::func() {
}
#endif WINDOW_H
Import_LP.h:
#ifndef IMPORT_LP_H //works best if this is first
#define IMPORT_LP_H
#pramga once
class Window;
class Import_LP {
void func(Window& parent); //parameter has to be a pointer or reference
};
#include "Window.h"
inline void Import_LP::func(Window* parent) {
}
#endif IMPORT_LP_H
This will only allow you to reference the other by pointer or reference until the actual include, but that should be doable. Technically you only have to do this to one or the other headers.
I was able to resolve the problem by random chance.
So apparently you can't have two files include each other?
I was doing
#include Window.h
in Import_LP.h
and
#include Import_LP.h
in Window.h.
I would appreciate it if someone could explain to me why you can't do this.
Sounds like the type Import_LP is undefined, your challenge is to figure out why. First thing to do is to read the contents of import_LP.h and figure out how Import_LP is declared. One possible approach would be to open one of the good files, right click on a use of Import_LP and select "Go To Declaration". That should move the focus to the declaration of Import_LP that applies in that specific case.
It could be the declaration is surrounded by a #ifdef that somehow prevents it being compiled in your other files. Or it could be something else, we don't really have enough details to work with.
Update:
So apparently you can't have two files include each other?
. . .
I would appreciate it if someone could explain to me why you
can't do this.
You can end up defining types and variables more than once when you do that.
There are techniques like #include guard and #pragma once which may be used to mitigate these sorts of problems.
I am sure this problem is asked a lot but I can't seem to find anything relevant. I have multiple source files in a C++ project. In typical fashion, there are multiple header files with class and function declarations and associated source files with their definitions. The problem is that when I try to use one of my classes defined in another file as a member for a class in a different file, I get compile errors even when uses the #include directive. What fixes the problem is by prototyping (is that the right word?) the class first before declaring it a member. So if ClassA is in one file and ClassB is in another and I want to use a ClassA member in ClassB I must write:
// ClassA.h
class ClassA {
public: ClassA (void); };
// ClassB.h
class ClassA; // prototype
class ClassB {
public: ClassA* ca; };
Is this normal? It doesn't matter if I use pointers or instances, I still must prototype them. I have found that I also must prototype structs and enums if they are in separate files. I can't seem to use constants declared using #define, or const across multiple files I get errors that they are undefined so I am not sure how to give them more than file scope. The same goes for typedefs. I am sure there is some easy fix to this that I am not remembering... any help is appreciated!
The word you’re looking for is “declare”, not “prototype”.
In any case, that’s normal. Usually you’d just include the relevant header files:
// ClassB.h
#include "ClassA.h"
class ClassB {
public: ClassA* ca; };
This will cause problems with circular references, though (but in all other cases it’s fine).
Another thing: Don’t forget your include guards, to protect against multiple inclusion of a file. That is, always write header files in the following way:
#ifndef UNIQUE_IDENTIFIER_HERE
#define UNIQUE_IDENTIFIER_HERE
// Rest of header file here.
#endif // ndef UNIQUE_IDENTIFIER_HERE
The UNIQUE_IDENTIFIER_HERE is usually an all-uppercase variant of the header file name, e.g. ‹PROJECTNAME›_‹PATH_TO_HEADER›_‹HEADERNAME›_H. For example, I’m currently working in a project (called “SeqAn”) that has a header file in the path parallel/taskdata.h. The unique identifier I use is thus SEQAN_PARALLEL_TASKDATA_H.
Yes it is obvious way.
This approach is most suitable when you're trying to avoid mutual dependencies among headers. If you're sure that no mutual dependency appears between two headers it is normal that you #include one in another.
To expand on Konrad's answer:
Yes, that's fine and dandy when you don't have to deal with circular references. If you do have to worry about circular inheritance, the diamond problem, or anything of the sort, here's a solution I found in someone else's question:
// ClassA.h
class ClassB;
class ClassA {
public:
ClassB* cb;
};
// ClassA.cpp
#include "ClassA.h"
#include "ClassB.h"
// code
// ClassB.h
#include "ClassA.h"
class ClassB {
public:
ClassA* ca;
};
// ClassB.cpp
#include "ClassB.h"
// code
As long as ClassA only tries to access members of ClassB inside member functions, and the member functions are all in the source file, you can just use a class declaration in the header, and leave the #include for the source file, where ClassA.h's include guard will solve the problem for you.