Regarding header files in C++ - c++

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"

Related

How to properly include the same .h file in two separate .cpp files?

I have a project consisting of 6 files; main.cpp, functions.h, tennisplayer.h, tennisplayer.cpp, tennisteam.h & tennisteam.cpp which are roughly defined as follows:
// main.cpp
#include "tennisteam.h"
#include <iostream>
#include <string>
#include <exception>
// some main() code that needs functions.h definitions
// functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include "tennisplayer.h"
#include <iostream>
#include <string>
#include <limits>
// some constants & function definitions needed by both main.cpp & tennisteam.cpp
#endif
// tennisplayer.h
#ifndef TENNISPLAYER_H
#define TENNISPLAYER_H
#include <string>
#include <vector>
// tennisplayer class declarations
#endif
// tennisplayer.cpp
#include "tennisplayer.h"
#include <iostream>
#include <fstream>
// tennisplayer class definitions
// tennisteam.h
#ifndef TENNISTEAM_H
#define TENNISTEAM_H
#include "tennisplayer.h"
#include <string>
#include <vector>
//
#endif
// tennisteam.cpp
#include "tennisteam.h"
#include <iostream>
#include <fstream>
// tennisteam class definitions
However, when I include functions.h into both main.cpp & tennisteam.cpp via tennisteam.h I get a linker error along the lines of:
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccv30cX0.o:tennisteam.cpp:(.text+0x0): multiple definition of `function(std::string const&)'; /tmp/ccRThgpp.o:main.cpp:(.text+0x0): first defined here
I'm aware this is a linker error. I've looked around for a fix but all I come across are posts instructing me to use include guards which I have done already. Is there something I'm missing here? Any help would be appreciated.
You have function function(std::string const&) that you not only declared but also defined in your header file. If you need to have it defined there instead of a .cpp file, mark it as inline.
This results in two cpp files (namely main.cpp and tennisteam.cpp) ending up with a definition of that function, because they both include that header file.

can someone help me with the #include being nested to deeply error before i go insane? thx [duplicate]

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.

Swig , C++ "include nested too deeply"

I am a beginner at c++ and I'm having this problem with nested include files. The code is too big to put here, but this is the part that I'm getting the error:
cvblob.h
#ifdef SWIG
%module cvblob
%{
#include "cvblob.h"
%}
#endif
#ifndef CVBLOB_H
#define CVBLOB_H
#include <iostream>
#include <map>
#include <list>
#include <vector>
#include <limits>
#include <opencv2/opencv.hpp>
#ifndef __CV_BEGIN__
#define __CV_BEGIN__ __BEGIN__
#endif
#ifndef __CV_END__
#define __CV_END__ __END__
#endif
#ifdef __cplusplus
extern "C" {
#endif
...
At line 4 in the above code (#iclude "cvblob.h"), the error happens:
[package_tracking/cvblob/cvblob.h:26]: (error) #include nested too
deeply
The guards are already used, but the error doesn't go away. Sorry that I am not able to put the entire code. If it is not possible to figure it out without the entire code, please answer these questions:
Should I put the guards (#ifndef CVBLOB_H) before the first line?
Is it necessary to put the guards in all the header files?
Thank you! I appreciate any suggestions.

C++ header guard not working

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

Blitz++ arrays as global arrays

I am working on a project in C++ which has many functions. I don't want to write them in the main program and would like to write a separate .cpp file for each of the functions. Most of these functions will act on some arrays, so I wish to make these arrays global. So I declared all the arrays in a separate .cpp file called globals.cpp and put them in a globals.h file prefixed with extern. The I wrote the functions and the main program as usual, however when I compile, I get an
Here is what I have:
//globals.cpp
#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"
BZ_USING_NAMESPACE(blitz)
Array<double,2> A(5,5);
In the globals.h file I have
#ifndef GLOBALS_H
#define GLOBALS_H
extern Array<double,2> A(5,5);
#endif
Then I have a function add.cpp, for example
#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"
BZ_USING_NAMESPACE(blitz)
void add.cpp(){
for(int i=0;i<5;i++){
A(i,i)=i*i;
}
}
I obviously include it in the prototype.h file
#ifndef GLOBALS_H
#define GLOBALS_H
void add();
#endif
Finally I have the main program mainprog.c
#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"
BZ_USING_NAMESPACE(blitz)
int main(){
add();
cout<<A<<endl;
return 0;
}
However when I compile I get the error `globals.h:6:8: error: ‘Array’ does not name a type
and then an error in the add.cpp function saying the error A was not declared.
How do I declare the blitz arrays as global?
Thank you
`
The issue is that your macro to import the namespace(BZ_USING_NAMESPACE) is below your include of globals.h. Thus the Array class you are trying to reference in globals.h is actually blitz::Array or something at that point.
For a simple fix, simply use the BZ_USING_NAMESPACE in globals.h right above your declaration for A.
Always remember to include everything a header file needs in that header file.
#ifndef GLOBALS_H
#define GLOBALS_H
#include <blitz/blitz.h> //No idea if the Array class needs this header.
#include <blitz/array.h>
BZ_USING_NAMESPACE(blitz)
extern Array<double,2> A(5,5);
#endif