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
Related
I am getting this compiler error
error: 'RawLog' does not name a type
Here is the relevant code:
//DataAudit.h
#ifndef DATAAUDIT_H
#define DATAAUDIT_H
class RawLog;
class DataAudit
{
...
private:
RawLog* _createNewRawLog(); // This is the line indicated with the error
};
#endif // DATAAUDIT_H
Usually a forward declaration resolves this kind of error. This answer indicates that a circular header inclusion may cause this. But doesn't the use of the #ifndef and #define statements prevent circular header inclusion?
Is there another reason I might see this error?
What are some avenues of approach I could use to further deduce the nature of this error?
Update: This is rather odd. I have a Globals.h file, and if I define a new enum in Globals.h, the error appears. Then if I comment out the enum, the error goes away. This leads me to think that the circular dependency has existed for a while, and adding the enum somehow re-orders the compilation units, thus exposing the dependency that wasn't there before?
The #ifndef header guard doesn't prevent circular dependencies. It just prevents multiple inclusions of the same header in a single file.
Looks like a circular dependency to me. This means you #include a header in DataAudit.h that #includes DataAudit.h either directly or indirectly.
In the end, I am not sure I understand completely why the error occurred, but this is what I did to resolve it, and some other related information. Maybe this will help others that come across this question.
For each header file in my project
For each #include "..." in the header
If there are no references to the class in the #include, remove it.
If there are only pointers to the class defined in the #include, then replace it with a class ... forward declaration.
If there is a member instance of the class defined in #include and it makes sense to use a pointer and allocate the member on the heap, then change the member to a pointer, and replace the #include with a class .... forward declaration.
Go through my Globals.h and move anything that can be moved out of it to more localized and specific header files.
Specifically, remove an enum that was defined is Globals.h, and place it in a more localized header file.
After doing all this I was able to make the error go away. Strangely enough, the enum in Globals.h seemed to be a catalyst for the error. Whenever I removed it from Globals.h, the error would go away. I don't see how this enum could cause the error, so I think it indirectly led to the error somehow. I still wasn't able to figure out exactly how or why, but it has helped me for this guideline when coding in C++:
Don't put anything in a header file unless it needs to be there. Don't
place anything in a Globals.h that can be placed in a more localized
file. Basically, do all you can to reduce the amount of code that is
included through the #include directives.
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.
In C++, I have a problem with a double include:
File stuffcollection.h
#pragma once
#ifndef STUFFCOLLECTION_H
#define STUFFCOLLECTION_H
#include "Stage.h"
class Stuffcollection {
public:
bool myfunc( Stage * stage );
};
#endif // STUFFCOLLECTION_H
File stage.h:
#pragma once
#ifndef STAGE_H
#define STAGE_H
#include "Stuffcollection.h"
class Stage {
// stuffcollection used in stage.cpp
};
#endif // STAGE_H
Compiler Error:
\Stuffcollection.h|(line were bool myfunc is declared)|error: 'Stage' has not been declared|
||=== Build finished: 1 errors, 0 warnings ===|
Can someone please explain why this happens and how it can be solved? I already use include guards and the pragma once preprocessor directive and it just doesn't work.
(If I remove #include "Stuffcollection.h" from stage.h and comment out the respective lines that are using it in stage.cpp, the rest of my code works fine. It's really just when including Stuffcollection into stage that it suddenly stops working.)
PS: stage is just one example, I use stuffcollection in almost every other file too, and everytime I get this problem.
EDIT: I followed what has been suggested, and now the problem is invalid use of incomplete type, i.e. while the answers given solve the problem of the circular dependency they do not solve the problem I am dealing with. My problem is continued in Circular Dependencies / Incomplete Types.
EDIT: Both solved now.
You have a Circular Dependency. Instead use Forward Declaration in Stuffcollection.h
#pragma once
#ifndef STUFFCOLLECTION_H
#define STUFFCOLLECTION_H
//#include "Stage.h" //<------------------Don't Do This
class Stage; //<------------------Do This
class Stuffcollection {
public:
bool myfunc( Stage * stage );
};
#endif // STUFFCOLLECTION_H
Rationale:
You can use the forward declaration in above snippet because, Stuffcollection.h only uses pointer to Stage.
Explanation:
Using a forward declaration of class Stage, the compiler does not know the composition of it nor the members inside it, the compiler only knows that Stage is a type. Thus,
Stage is an Incomplete type for the compiler. With Incomplete types , One cannot create objects of it or do anything which needs the compiler to know the layout of Stage or more than the fact that Stage is just an type. Since pointers to all objects need just the same memory allocation, You can use the forward declaration when just referring to Stage as a pointer.
You can use Forward Declarations to get over your circular dependency problems.
Further Read:
When to use forward Declarations?
It's a circular dependency. Don't make them. This one you can resolve by replacing #include "Stage.h" in stuffcollection.h with a forward declaration of Stage (i.e. class Stage;).
Instead of the #include you can use forward declarations i.e. class Stage; and class Stuffcollection. It also has the benefit of reducing dependencies.
It's a well known issue this damn error
expected class-name before ‘{’ token
Well, despite my hard working and googling, I could not solve this error. Sorry. This is my last shore.
In ui.cpp of a project of mine I do:
#include "wfqueue_proxy_factory.hpp"
OK, this raises this stupid error in my compiler:
In file included from
wfqueue_proxy_factory.hpp:29,from
ui.cpp:28:
wfqueue_manager_proxy.hpp:42: error:
expected class-name before ‘{’ token
There are three classes in my project:
First
// wfqueue_proxy_factory.hpp
#ifndef _WFQUEUE_PROXY_FACTORY_HPP
#define _WFQUEUE_PROXY_FACTORY_HPP
#include "wfqueue_manager_proxy.hpp"
// ...
class WFQueueProxyFactory {
//...
};
#endif
Second
// wfqueue_manager_proxy.hpp
#ifndef _WFQUEUE_MANAGER_PROXY_HPP
#define _WFQUEUE_MANAGER_PROXY_HPP
#include "workflow.hpp"
#include "wfqueue.hpp"
// ...
class WFQueueManagerProxy : public WFQueue { // This is the problem (line 42)
//...
};
#endif
Third
// wfqueue.hpp
#ifndef _WFQUEUE_HPP
#define _WFQUEUE_HPP
#include "workflow.hpp"
class WFQueue {
// ...
};
#endif
PLEASE PLEASE PLEASE note that I use ; after } of every class, I checked out EVERY header in my project looking for this problem and didn't find any class not followed by ; after its closing bracket. This is valid for workflow.hpp which is a simple class (not deriving from any class, just a plain class).
WFQueue is some sort if interface, I use this pattern with other classes too and they work. WFQueue contains some virtual pure methods... problem should not be here anyway.... I suppose this because I use another "interface" class with other classes and they work fine.
This error disappears if I do this:
// wfqueue_manager_proxy.hpp
#ifndef _WFQUEUE_MANAGER_PROXY_HPP
#define _WFQUEUE_MANAGER_PROXY_HPP
#include "workflow.hpp"
#include "wfqueue.hpp"
// ...
class WFQueueManagerProxy {
//...
};
#endif
Don't really know how to solve this problem... please help me.
Thank you
You should run the preprocessor on your code but not compile it, and examine the result. To do this, copy the command which runs the failing compilation, and with most compilers you'd then remove the -o outfile option and add something like -E (see your compiler's documentation for the flag which does preprocessing only).
The compiler will emit (on stdout) the entire translation unit with all #includes and such resolved, so you can clearly see what is missing (just search for the line of code that matches the error line, then look up to see what declarations you find). If it's still not clear what the problem is, write the preprocessed output to a file and try compiling that. You can then tweak the preprocessed source and see what's needed to fix it.
Just a wild guess: Your error says that in
class WFQueueManagerProxy : public WFQueue { // This is the problem (line 42)
//...
};
there must be a class name before {. Therefore I assume that the compiler doesn't know that WFQueue is a class. Are you sure that its definition is included? I mean, maybe in wfqueue.hpp the class is named WfQueue or different in some other way?
The problem might be in misnamed include guards. Try to check if they are really unique per file. It seems that you made it to disable the definition of WFQueue while compiling WFQueueManagerProxy.
It's something it never happened... my god sorry...
It seems that my virtual machine backup disk collided with the original one. I run my project on a virtual machine, making the backup, 2 hours ago, probably messed up something... I adjusted it and now the virtual machine can locate the correct folder and the correct files to compile. It was amazing ahaha and obvious, the ols files g++ tried to compile where a previous version filled with mistakes... This was one of that bugs... a guard header repeated.
Icecrime was right... despite I looked for repetitions in my files, in the previous version, where I didn't fix this problem, there were some files I pasted and forgot to change guard header.
Thank you everyone for your patience and effort.
I'm sorry I didn't notice this very strange virtual disk collision in my machine. Thanks again.
Make sure you typed
using namespace omnetpp;
after includes. It solved my problem.
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.