Swig , C++ "include nested too deeply" - c++

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.

Related

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.

How do I solve this error: include files are nested too deeply

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

Regarding header files in 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"

aggregate ‘arcball arcball’ has incomplete type and cannot be defined

I'm having a bit of a problem on the following files:
i have the arcball struct on this file:
#ifndef ARCBALL_H_
#define ARCBALL_H_
#include "ex1.h"
...
extern struct arcball{
float radius;
int x,y;
}arcball;
...
#endif /* ARCBALL_H_ */
and I have the following ex1.h file which includes the arcball.h file:
#ifndef __EX1_H__
#define __EX1_H__
////////////////////////////
// Project Includes
////////////////////////////
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include "arcball.h"
////////////////////////////
// OpenMesh Includes
////////////////////////////
#include "OpenMesh/Core/IO/MeshIO.hh"
#include "OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh"
////////////////////////////
// GL Includes
////////////////////////////
#include "GLee.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
...
struct arcball arcball;
#endif
I have to include the ex1.h header since the ex1.h holds includes for glut functions I
use in the arcball.cpp file as well.
and I have to use the arcball.h header in order to use the functions and the struct
defines on that file.
The error I get is the following:
In file included from arcball.h:11:0,
from arcball.cpp:8:
ex1.h:120:16: error: aggregate ‘arcball arcball’ has incomplete type and cannot be defined
make: *** [ex1] Error 1
I don't understand why it is an incomplete type, since I included arcball.h file
Is this an mutual inclusion issue or a struct definition/usage issue?
And how can it be resolved?
Thanks!
The two .h files include each other, causing much of the confusion. That's a terribly bad idea, as the #ifdef around will have different effects depending on which file is included first: in this case, arcball.h -> ex1.h -> arcball.h-but-really-nothing-because-of-the-#ifdef.
Moreover, it is also a bad idea to declare a variable without extern in a header (here ex1.h). That's probably not having the effect that you want. Variables without extern should only be declared in .c files.

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