I'm trying to get my includes working, but everything I try leads to errors. Even using #pragma once doesn't work. Do you know what I made wrong?
main.cpp
#include "utility/headers/Window.h"
#include "engine/headers/Player.h"
#include "engine/headers/Chunk.h"
ChunkManager.h
#ifndef CHUNK_MANAGER_H
#define CHUNK_MANAGER_H
#include "../../utility/headers/Vector3i.h"
#include "Chunk.h"
#include <map>
class ChunkManager{...}
#endif // CHUNK_MANAGER_H
Chunk.h
#pragma once
#ifndef CHUNK_H
#define CHUNK_H
#include <glm/glm.hpp>
#include "CubeCreator.h"
#include "ChunkManager.h"
#include "../../utility/headers/Random.h"
#include "../../utility/noise/headers/Noise.h"
class Chunk{...}
#endif // CHUNK_H
Error message is 'ChunkManager' has not been declared.
Thanks in advance!
Replace #include "ChunkManager.h" with class ChunkManager;.
That's called forward declaration and solves problems like class A needs to know about class B and class B needs to know about class A.
Depending on how you use ChunkManager in class Chunk. A forward declaration might not work.
Since non of the mentioned techniques worked in my case I defined a new header file containing global variables like chunkSize. Maybe it's just impossible to do what I've tried.
However for those who might find this question here's how my imports look now:
ChunkManager.h
#include "Chunk.h"
Chunk.h
// no includes
main.cpp
#include "ChunkManager.h"
And access to chunkSize isn't done anymore by calling ChunkManager::chunkSize but instead by calling Settings::chunkSize
Related
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed last year.
//main.cpp
#include <iostream>
#include <array>
#include <iomanip>
#include "Board.h"
#include "Game.h"
//Player.h
#include <array>
#include <string.h>
#include <random>
#include "Property.h"
#include "Game.h"
#ifndef Player_h
#define Player_h
//Property.h
#include "Space.h" //#include nested too deeply error
#ifndef Property_h
#define Property_h
//Space.h
#include <sstream>
#include <string>
#ifndef Space_h
#define Space_h
//FreeParking.h + a couple others inheriting from space; there's no error in any of these either
#include "Space.h"
#ifndef FreeParking_h
#define FreeParking_h
//Board.h
#include "Player.h"
#include <array>
#include <random>
#ifndef Board_h
#define Board_h
//Game.h
#include <array>
#include "Property.h"
#include "CommunityChest.h"
#include "Tax.h"
#include "FreeParking.h"
#include "Jail.h"
#include "GoToJail.h"
#include "Go.h"
#include "Player.h"
#ifndef Game_h
#define Game_h
I don't think I made any changes to the #includes today but just got this error even though the program was running fine 20 minutes ago. I only posted the includes because I'm not sure if the actual code matters for this error or not. If it does I'll try to go over the stuff I wrote today, but most of it was just changing things that already worked previously.
Player.h includes Game.h and Game.h includes Player.h. This is an infinite loop. There might be more, but that's just the first one I saw.
You should remove at least one of those includes to break the infinite loop. If you get errors when you do that, you might be able to fix them using a forward declaration that looks something like this:
class Player;
A forward declaration like that would allow you to compile some code that uses the Player class, even though a complete definition of the Player class is not available at that point in the program.
Two more tips to make things more sane:
Put your include guards at the very top of your file before you include anything.
Use #pragma once as the include guard instead of your more complicated thing.
Should I put also my headers, and not only the classes inside the ifndef, define etc?
For example I have, this is just an example of code. Now, my question is, should I put the #include "myfile.h" in the ifndef, or should it stay outside?
#include <iostream>
#include "myfile.h"
#ifndef ANOTHERFILE_H
#define ANOTHERFILE_H
struct node
{
int val;
node*next;
}
#endif //ANOTHERFILE_H
I keep getting this error:
QuadraticProbing.h:54:22: error: ‘Human’ has not been declared
int hash(Human &human, int tableSize );
However, in QuadraticProbing.h, I #include at the top familytree.h, in which the class Human is declared. Does anyone know why I am still getting compilation errors? I think it has to do with multiple redefinition, because in familytree.h, I also #include QuadraticProbing.h because I use some of those functions in the corresponding.cpp file. Here is what I have at the top of each file. Any input would be greatly appreciated!! =]
#ifndef _QUADRATIC_PROBING_H_
#define _QUADRATIC_PROBING_H_
#include "vector.h"
#include "mystring.h"
#include "familytree.h"
----------------------
#ifndef FAMILYTREE_H
#define FAMILYTREE_H
#include "QuadraticProbing.h"
#include "familyRunner.h"
I'm having this issue where I cannot call the object constructor in main.cpp even after it has been included in main.h. The error message is:
C:\Users\Espresso\Projects\AZRA\Debug/../src/main.cpp:7: undefined reference to `g_editor::LevelEditor::LevelEditor()'
Where main.cpp contains
#include "main.h"
g_editor::LevelEditor g_levelEditor;
and main.h contains:
#include "g_editor/g_editor.h"
g_editor.h contains all of the header files of the objects in the library, which includes the levelEditor. g_editor.h:
#ifndef G_EDITOR_H_
#define G_EDITOR_H_
#pragma once
#include "g_editor/Objects/editor_module.h"
#include "g_editor/Objects/utility_window.h"
#include "g_editor/Objects/prompt_window.h"
#include "g_editor/LevelEditor/LevelEditor.h"
extern g_editor::LevelEditor g_levelEditor;
#endif
And finally, LevelEditor.h contains the constructor and member functions of LevelEditor:
#ifndef G_LEVEL_EDITOR_H_
#define G_LEVEL_EDITOR_H_
#pragma once
#include "../Objects/editor_module.h"
#include "Modules/collisionGrid_module.h"
#include "Modules/HUD_module.h"
#include "Modules/IO_module.h"
#include "Modules/ledge_module.h"
#include "Modules/segment_module.h"
#include "g_level/g_level.h"
using namespace g_level;
namespace g_editor
{
class LevelEditor
{
private:
std::vector<editor_module*> modules;
void loadModules();
public:
static LevelEditor& get()
{
static LevelEditor sSingleton;
return sSingleton;
}
LevelEditor();
~LevelEditor() {};
I apologize for the wall of text, I've been staring at this for a few days now and I have tried reordering the static libraries by precedence (which eliminated all issues save for this one.) Is there a design flaw in my current setup? I am using sSingletons, global externs, and static libraries.
There is no definition of LevelEditor::LevelEditor.
You are either missing a source file, or you forgot to add {}.
Edit: or, if your constructor does not do anything anyway, just remove the declaration.
Either
1) This function is missing:
LevelEditor(); // So now what does this do???? That's what is missing.
or
2) it isn't missing, but you didn't add the source module or library where this function is located to your linker settings.
#ifndef _MY_OPENCLPLATFORM_
#define _MY_OPENCLPLATFORM_
#include "OpenCL.h"
namespace my
{
class OpenCLPlatform
{
cl_platform_id mplatformID;
cl_uint mnumDevices;
std::vector<OpenCLDevice> mdevices; // OpenCLDevice was not declared in this scope
public:
OpenCLPlatform(cl_platform_id platformID);
void getDevices();
void printInfo();
cl_platform_id& getPlatformID();
};
}
#endif
#ifndef _MY_OPENCLDEVICE_
#define _MY_OPENCLDEVICE_
#include "OpenCL.h"
namespace my
{
class OpenCLDevice
{
cl_device_id mdeviceID;
public:
OpenCLDevice(cl_device_id device);
void printInfo();
void printDeviceType(cl_device_type deviceType);
};
}
#endif
#ifndef _MY_OPENCL_
#define _MY_OPENCL_
#if defined(__APPLE__) || defined(MACOSX)
#include <OpenCL/opencl.h> // This works only for XCODE compiler
#else
#include <CL/cl.h>
#endif
#include <cassert>
#include <iostream>
#include <vector>
#include "Exception.h"
#include "OpenCLDevice.h"
#include "OpenCLPlatform.h"
namespace my {
class OpenCLDevice;
class OpenCLPlatform;
class OpenCL;
class OpenCL
{
cl_uint mnumPlatforms;
std::vector<OpenCLPlatform> mplatforms;
void getPlatforms();
public:
OpenCL();
~OpenCL();
void quickSetup();
void printPlatformVersions();
};
}
#endif
Does the the ordering "class OpenCLDevice; class OpenCLPlatform; class OpenCL;" matter? Sometimes, header files depend on each other which can lead to "hard to follow" or convoluted inclusions...Do you have a "one way" technique to deal with convoluted inclusions that you use all the time?
Edit:
I changed the code to match my real problem. If you look at the code above, the compiler is saying that 'OpenCLDevice was not declared in this scope'.
Edit:
I finally got the code to work, and this is what I did:
1. add #include "OpenCLDevice.h"in OpenCLPlatform.h
2. compile
3. remove #include "OpenCLDevice.h"in OpenCLPlatform.h
4. compile
It works now!
Edit:
I cleaned the project and removed all dependencies, and I'm getting the same errors again.
Edit:
I think compiler did something to the code. It may have chose to not include libraries that aren't used in the header and source file, but are used in other headers and source codes
Since you are including classa.h and classb.h where both classes are (presumably) defined, you shouldn't even need the forward declaration.
However, if you did not include them, then no, order of the declarations wouldn't matter. As long as as a class is forward declared before it is used you should be OK.
I see two potential issues:
Your #include "OpenCL.h" may not include the file you expect (yours), but instead some system file.
Forward declarations can't be used in your case. It works only when you have pointers or references to class instances. Your vector<OpenCLPlatform> requires the class declaration (i.e. inclusion of the corresponding header).