I have three classes.
first class:
#ifndef C_LINKED_LIST_H
#define C_LINKED_LIST_H
class CLinkedList {
private:
//removed code for brevity
public:
// removed code for brevity
};
#endif
second class:
#ifndef C_SSF_FOLDER_CONTAINER_H
#define C_SSF_FOLDER_CONTAINER_H
#include "C_SSF_Folder.h"
#include "CLinkedList.h"
class C_SSF_Folder_Container {
private:
// removed code for brevity
public:
int Add_Folder(C_SSF_Folder *_pcl_SSF_Folder);
C_SSF_Folder *Get_Folder(int _i_Index);
C_SSF_Folder *Get_Folder(char *_pch_Name);
//^-----errors
};
#endif C_SSF_FOLDER_CONTAINER_H
my third class
#ifndef C_SSF_FOLDER_H
#define C_SSF_FOLDER_H
#include <windows.h>
#include <fstream>
#include "C_SSF_Folder_Container.h"
using namespace std;
class C_SSF_Folder {
public:
private:
C_SSF_Folder_Container cl_SSFFC_Folder_Container;
public:
};
#endif
my third class C_SSF_Folder.
I am including "C_SSF_Folder_Container.h"
and declaring a C_SSF_Folder_Container container.
Before declaring the variable it compiles fine. After I declare it
I get syntax errors in my C_SSF_Folder_Container
Severity Code Description Project File Line Suppression State
Error C2061 syntax error: identifier 'C_SSF_Folder' CSSFileSystem\projects\cssfilesystem\cssfilesystem\c_ssf_folder_container.h 16
Error C2061 syntax error: identifier 'C_SSF_Folder' CSSFileSystem \projects\cssfilesystem\cssfilesystem\c_ssf_folder_container.h 19
As I myself look into it I think there is a problem because my C_SSF_Folder is including C_SSF_Folder_Container.
and C_SSF_Folder_Container is including C_SSF_Folder
but the defines should take care of it? Other than that I have no clue what's the problem.
Everything is typed correctly.
You've got a circular #include -- C_SSF_Folder_Container.h #includes C_SSF_Folder.h and C_SSF_Folder.h #includes C_SSF_Folder_Container.h.
This would cause an infinite regress (and a compiler crash) except that you've got the #ifndef/#define guards at the top of your files (as you should); and because of them, instead what you get is that one of those two .h files can't see the other one, and that's why you get those errors.
The only way to fix the problem is to break the circle by deleting one of the two #includes that comprise it. I suggest deleting the #include "C_SSF_Folder.h" from C_SSF_Folder_Container.h and using a forward declaration (e.g. class C_SSF_Folder; instead.
C_SSF_Folder.h and C_SSD_Folder_Container.h are including each other(Circular Dependency).
When the compiler compiles C_SSF_Folder_Container object, it needs to create a C_SSF_Folder object as its field, however, the compiler needs to know the size of C_SSF_Folder object, so it reaches C_SSF_Folder object and tries to construct it. Here is the problem, when the compiler is constructing C_SSF_Folder object, the object has a C_SSF_Folder_Container object as its field, which is a typical chicken and egg question, both files depends on each other in order to compile.
So the correct way to do it is to use a forward declaration to break the circular dependency(including each other).
In your C_SSF_Folder.h, make a forward declaration of C_SSF_Folder_Container.
#include <windows.h>
#include <fstream>
using namespace std;
class C_SSF_Folder_Container;
class C_SSF_Folder {
public:
private:
C_SSF_Folder_Container cl_SSFFC_Folder_Container;
public:
};
#endif
Finally, include C_SSF_Folder_Container.h in your C_SSF_Folder.cpp.
You can also learn more in the following links:
Circular Dependency (Wiki):
https://en.wikipedia.org/wiki/Circular_dependency
Forward Declaration by Scott Langham
What are forward declarations in C++?
Related
I'm having some troubles where a function isn't returning the right type, because a class isn't defined. I'm using a factory pattern.
The two error messages that I'm getting are:
'return': cannot convert from 'DLA *' to 'Layer *'
and:
'Layer': base class undefined (compiling source file src\Layer.cpp)
and this same error message is repeated for every file that includes Layer.h.
Here is what my class that inherits from Layer looks like (DLA.h):
#pragma once
#ifndef _DLA
#define _DLA
#include "ofMain.h"
#include "ofxGui.h"
#include "Layer.h"
class DLA: public Layer
{
public:
DLA();
void setup();
void update();
void draw();
private:
};
#endif
and here is my Layer class header (Layer.h):
#pragma once
#ifndef _LAYER
#define _LAYER
#include "ofMain.h"
#include "ofxGui.h"
#include "DLA.h"
enum SceneType
{
Scene_None,
Scene_Default,
Scene_DLA,
};
class Layer
{
public:
void setup();
void update();
void draw();
static Layer *CreateSimulation(SceneType Type);
private:
};
#endif
The function which is failing is this one, situated in Layer.cpp:
Layer *Layer::CreateSimulation(SceneType Type)
{
switch (Type)
{
case Scene_None:
default:
return nullptr;
case Scene_DLA:
return new DLA();
}
}
I've tried everything I could find on Stack Overflow that had similar issues to mine but I've seen some people recommend very subtle code indentation to fix this, so I'm really lost as to find where the problem is.
As they stand, your header files induce circular dependency, even though the #pragma once (and other) guards prevent any actual 'infinite recursion'. Let's look at the sequence of code, from the compiler's point-of-view, when compiling the Layer.cpp file (or any other '.cpp' source that has #include "Layer.h" in it).
The compiler encounters #include "Layer.h" (the first time it has done so - the guards won't be 'triggered'), so it duly replaces that line with the contents of the indicated header. In that content, it encounters #include "DLA.h" (we can ignore the other headers included in this discussion, assuming that they aren't relevant to the problem in hand). So, it then duly replaces that line with the contents of the DLA.h header, at which point it will come across this:
#include "Layer.h"
class DLA: public Layer
{
Now, here, when it replaces #include "Layer.h" with the header content, that content will be 'empty' (because of the guards, as it has already included that header once). Thus, when the public Layer code is encountered, it is an error, because that class has not yet been defined, or even declared as a class.
So, if you really insist on having the #include "DLA.h" line in Layer.h, then it must be placed after the definition of the Layer class.
However, a far better way would be to remove #include "DLA.h" from Layer.h, and only place it in source (.cpp) files that actually need it (like Layer.cpp). This would work well:
// Layer.cpp
#include "Layer.h"
#include "DLA.h" // At this point, references to the Layer class in DLA.h will be fine!
//...
Layer *Layer::CreateSimulation(SceneType Type)
{
switch (Type)
{
case Scene_None:
default:
return nullptr;
case Scene_DLA:
return new DLA();
}
}
Feel free to as k for any further clarification and/or explanation.
I have a dialog window settings that I want to display all values from my breadData object, and to do so I want to have settings inherit breadData's protected members. I try to forward declare breadData, but I'm getting a couple errors in my code.
/home/--/breadPull/prj/settings.h:14: error: invalid use of incomplete type 'struct breadData'
/home/--/breadPull/prj/resultwnd.h:7: error: forward declaration of 'struct breadData'
For one, breadData is not a struct, why does the compiler think breadData is a struct? Secondly I don't understand what the second line is trying to say. My only guess is because there are a lot of circular dependencies in my program. Here's the relevant code:
settings.h
#include <QDialog>
#include "breaddata.h"
class breadData;
namespace Ui {
class Settings;
}
class Settings : public QDialog, public breadData
{
Q_OBJECT
//.....
breadData.h
#include <vector>
#include <string>
#include <QtWidgets>
#include <QMainWindow>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "resultwnd.h"
#include "settings.h"
class MainWindow;
class resultWnd;
class breadData
{
public:
breadData(std::string);
~breadData();
//read in data file that provides all information
bool readData();
//.......
resultWnd.h
include <QGroupBox>
#include "breaddata.h"
class breadData;
namespace Ui {
class resultWnd;
}
class resultWnd : public QGroupBox
//.....
You have a circular dependency. breaddata.h includes settings.h before declaring breaddata. settings.h requires the declaration of breaddata for the inheritance.
Thus the file the preprocessor creates when compiling a file that includes breaddata first looks like this (indentation to visualize the recursive insertion of included header files):
<content of breaddata.h>:
<content of vector, string, QtWidget, QMainWindow, mainwindow.h and ui_mainwindow.h>
...
<content of resultWnd>:
...
class breaddata; //forward declaration mentioned in the error message
...
<content of settings.h>:
...
class Settings : public QDialog, public breadData //DANG!
...
class breaddata { ... //too late
Summarized:
class breaddata; //forward declaration mentioned in the error message
...
class Settings : public QDialog, public breadData //DANG!
...
class breaddata { ... //too late
The solution here is to avoid the includes in breaddata.h, especially settings.h. If necessary, forward-declare Settings. The rule of thumb is to include in headers only if you must, and forward-declare whenever you can.
Your problem is that you have an incomplete understanding of the following:
The precompiler
The difference between declarations and definitions and purpose of using the former.
The purpose and use of namespaces
Without knowing more about your code than what you've shown, the following should solve your problem:
settings.h
#ifndef SETTINGS_H
#define SETTINGS_H
// Your code as above
#endif
breaddata.h
#ifndef BREADDATA_H
#define BREADDATA_H
// Your code as above
#endif
resultWnd.h
#ifndef RESULTWND_H
#define RESULTWND_H
// Your code as above
#endif
I suspect that this will not solve your problem entirely though. Based on your second error message, I suspect you have left important lines of code out of your question so nobody will be able to give you a definitive answer to solve your problem.
I suggest you edit the code in your question to include all lines that contain the breadData, Settings and resultWnd
It is possible to solve this, we just need to see how the three classes are bound together so we can help you untangle them.
The reason the compiler thinks you're using a struct is purely historical. The class keyword was introduced in C++ with the intention to replace the struct. Previously, in C, only the struct keyword exists. As far as I know, the only difference between structs and classes is the default access level. classes default to private while structs default to public. Otherwise, they have identical usage.
Edit: I have found my problem, even through I asked a crappy question, I found circular inclusion and fixed it.
I am getting a strange error: error C2504: 'CUpdatable' : base class undefined. It is strange be cause in my code I have included both the base class header file, and even have the class prototype. Here are all of the relevant pieces of code:
In CRoom.h
#ifndef CROOM_H
#define CROOM_H
#include "CUpdatable.h"
class CUpdatable;
class CRoom : public CUpdatable // <-- error here "CUpdatable is undefined"
{
...
}
#endif
In CRoom.cpp
#include "stdafx.h"
#include "CRoom.h"
CRoom:: // ...
// etc
In CUpdatable.h
#ifndef CUPDATABLE_H
#define CUPDATABLE_H
class CUpdatable
{
...
}
#endif
In CUpdatable.cpp
#include "stdafx.h"
CUpdatable:: // ...
// etc
My first guess was to include the class prototype class CUpdatable; within CRoom.h, but that has not worked.
What looks like is happening is that the header files are not seeing the .cpp files at compile time, so CUpdatable does not have definitions at that point, and throws this error. So how do you fix this?
I'm trying to integrate c++ code with awesomium functionalities, but I get many errors.
It seems that VisualStudio doesn't like my definition/declaration of the WebCore element. I copied it from http://wiki.awesomium.com/tutorials/tutorial-1-hello-awesomium.html.
I have simplified the code until this, and I still get the errors.
SimpleClass.cpp:
#include <Awesomium/WebCore.h>
include "SimpleClass.h"
using namespace Awesomium;
CSimpleClass::CSimpleClass(){
WebCore *web_core = WebCore::Initialize(WebConfig());
}
CSimpleClass::~CSimpleClass(){
}
SimpleClass.h:
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
WebCore *web_core;
};
Thanks!
Change your SimpleClass.h header to read:
#pragma once
#ifndef SIMPLECLASS_H
#define SIMPLECLASS_H
// forward declarations
namespace Awesomium{
class WebCore;
}
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
Awesomium::WebCore *web_core;
};
#endif /* SIMPLECLASS_H */
That way you announce to your compiler that there exists a type WebCore in the namespace Awesonium, and then you can use it to declare the member pointer CSimpleClass::web_core.
Potential dependency issues aside, the problem is that your header does not know that you want to use the Awesomium namespace.
Either (preferred) be explicit in the header about your definition of *web_core by doing
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
Awesomium::WebCore *web_core; //note the use of Awesomium::
};
or (if you really must) include your header after your using directive
#include <Awesomium/WebCore.h>
using namespace Awesomium;
#include "SimpleClass.h"
I have a trouble with my c++ code. I know there is much advices with error "expected class-name before ‘{’ token" but I still can't find where I have it. Here is my sources:
Postava.h
#include <exception>
#include <string>
using namespace std;
#ifndef __Postava_h__
#define __Postava_h__
#include "Barva.h"
#include "Pozice.h"
//#include "Budova.h"
//#include "HerniEngine.h"
#include "GrafickyObjekt.h"
class Budova;
class HerniEngine;
//class GrafickyObjekt;
class Postava;
struct Barva;
struct Pozice;
class Postava:public GrafickyObjekt{ //<----- Here is the error
private:
std::string m_jmeno;
int m_nosnost;
public:
Postava(std::string jmeno, int nosnost);
Budova* m_Budova;
HerniEngine* m_HerniEngine;
std::string vratJmeno();
int vratNosnost();
void vykresli();
};
#endif
GrafickyObjekt.h
#ifndef __GrafickyObjekt_h__
#define __GrafickyObjekt_h__
#include "HerniEngine.h"
#include "Pozice.h"
#include "Posun.h"
class HerniEngine;
class GrafickyObjekt;
class Scena;
struct Pozice;
struct Posun;
class HerniEngine;
class GrafickyObjekt {
protected:
Pozice m_pozice;
public:
HerniEngine* m_HerniEngine;
// kazdy potomek, tj. graf. obj. ma pozici
GrafickyObjekt(Pozice pozice);
// vsichni potomci ji musi implementovat
virtual void vykresli() = 0;
// tyto metody nejsou (ciste) virtualni, budou normalne zdedeny
// tim mam zaruceno, ze vsichni potomci je maji
void pohni(Posun posun);
void pohni(Pozice pozice);
};
#endif
Sorry for my english and for names of classes and names of variables, it's in czech.
Thanks a lot for every advice.
Same answer as in all similar questions asked before:
You created a circular include sequence
It is not obvious from what you posted so far (since you haven't posted all headers). But it is a safe bet that your other header files taken together must produce a circular include "path". More precisely, your GrafickyObjekt.h somehow indirectly includes Postava.h (through other header files that you haven't posted).
The include guards you used in your header files will "break" that cycle in some unpredictable or (better word) unforeseen way. In your case the include guards caused Postava.h to get physically included first, which is why it knows nothing about GrafickyObjekt even though it seems to explicitly include GrafickyObjekt.h. Hence the error.
Circular includes make no sense and achieve nothing. You have to stratify your headers by levels - from low level to high level - and make sure that higher-level headers include lower-level headers, but never the other way around.
Once you achieve that sort of order, you can proceed to resolving circular data dependencies by introducing forward class declarations. I see that you already tried to do that and ended up with a total catastrophic mess, where you basically forward-declare all classes in all headers. Get rid of that mess and start over, by fixing the include stratification first.
It seems that some of your previous includes are faulty.
#include "HerniEngine.h"
#include "Pozice.h"
#include "Posun.h"