Are variables in a header file global scope? - c++

Say I have a header file to store an enum:
my_enum.h:
#ifndef my_enum_h
#define my_enum_h
enum my_enum {ONE, TWO};
#endif
Then I include this header file in the files game.h and game.cc. Finally, I include game.h in main.cc.
I can now use ONE and TWO in main.cc. Is it normal to have these variables globally defined?

When you include game.h in main.cc, because game.h already includes my_enum.h, it can be accessed from files who include game.h, aswell as including my_enum.h
Solution 1:
Remove "#include my_enum.h" from your code, as it doesn't seem as if it needed
Solution 2:
Maybe change the name of the enum/the values inside

If you include a header in your implementation file. It is the same as writing the exact code of header file in your implementation file.
Hence this enum declaration gets included in your game.h and subsequently in your main.cc.
If you want to remove it, you will have to remove it from the header file and declare it locally wherever you want to use it.

Related

Adding a common header file to all existing source and header through Eclipse

In Eclipse IDE(C++), how can I add a common header file to all existing source and header files? I have a common header file created say "common.h". This file needs to be included in all the source files (.c) as well all the header files (.h). Rather than manually adding #include "header.h", how can I do it through Eclipse in one go? My header "header.h" has
#ifndef HEADER_H
#define HEADER_H
....
#endif
I am basically looking for a short cut to avoid the manual task.
In fact, this is part of a voluminous exercise where I would like to replace all std:;cout with myout (say) where myout is an object of my own class MYIOSTREAM subclassed from iostream and common.h is the header containing that class declaration. Possible other that manually opening each of the files and ...

Custom header files and include libraries [C++]

If I create a header like this:
#ifndef _MY_HEADER_H
#define _MY_HEADER_H
#include <iostream>
void foo();
#endif
With it's correspondent .cpp file, do I need to include iostream in the main.cpp file?
To answer you question: No you don't need to include it (again).
But it is good practice, to include in the header only the stuff that is required for the header to work. So if your foo() method requires iostream, you should include it. If you create a class that uses only pointers or references to other classes you should prefer forward declarations over inclusion of the full-fledged header of the respective classes.
It is not necessary to include it again in main.cpp, as in main.cpp version if you include the .h version of the same ,the inclusion will automatically be available in compilation, why write an extra redundant line?

Problem with Class for processing of strings

I read lines from a text file and would like to repeated delimiters as \t\t. Normally I have between delimiters a parameter but for readability reasons it is sometimes nice to use strings as \t\t\t in order to line out text sequences.
I wrote a class in my main.cpp that worked well. Because I would like to keep my main.cpp as compact possible, I tried to create a class file with header file. I did the forward declaration in the header file, and pasted the working class member in the class.cpp file.
The Class uses string type variables that are declared in the class.cpp. When compiling the compiler gives me an error saying that "string does not name a type". I guess there is something wrong with the moment I do the include of the string.h header.
It is included in the Main.cpp file. Should I include it also in the header file for the class or in the class.cpp file. I understood from previous exchanges that including libraries everywhere should be avoided.
Thanks in advance,
Stefan
If you want to use C++ std::string you should include the <string> header.
The similarly named <string.h> is for C language string functions.
Header files should be self-contained, i.e. include all the things they require for themselves (like types they refer to). To prevent bad performance and other issues, so-called include guards prevent repeated inclusion.
You need to include the .h file in all other files (.h or .cpp) that use the types/functions you declare in the header.
class.cpp should include class.h. main.cpp should include class.h if it uses your string class.

C++ Header Guard issues

I am making a small C++ framework, which contains many .h and .cpp.
I have created a general include which include all my .h file such as:
framework.h
#include "A.h"
#include "B.h"
#include "C.h"
each .h header are protected with include guard such as
#ifndef A_HEADER
#define A_HEADER
...
#endif
The issues is, I would like to be able to include "framework.h" inside all the sub .h such as, but it cause lots of compiler error:
#ifndef A_HEADER
#define A_HEADER
#include "framework.h"
...
#endif
If instead I use the real header file for each sub header, and the framework.h for what ever use my framework it works fine..
I would just like to include the main header inside all my sub .h so I dont need to include all the dependency everytime.
Thanks :)
Basically what your doing is #include "A.h" in framework.h and #include "framework.h" in A.h. This causes cyclic dependency of the header files and you will get errors such as undefined class A. To solve this, use forward declarations in header file and #include only in corresponding cpp file. If that is not possible then I don't see any other option other than including individual header files.
Just protect the main header with include guards too:
#ifndef FRAMEWORK_H
# define FRAMEWORK_H
# include <A.h>
# include <B.h>
# include <C.h>
#endif
That will prevent recursive inclusion.
You should not including the main header file inside the sub-header files. It should be used to make user's life easier, not yours.
Instead do following:
1) Make forward definitions of all you need in the related sub-header files.
2) Include only needed sub-header files inside CPP files.
3) When using your framework inside an application code (for example), then you could include the main framework header file.
i would recommend using #pragma once, and placing that at the top of all of your header files (framework.h, A.h, B.h, and C.h).
Although, if you'd rather, I think you could fix your problem by simply putting an include guard in framework.h as well.
I guess you have a dependency between - say B and C such that B depends on C, but in framework.h C is included after B.
Circular includes are generally a bad idea in C++. While having header guards will prevent the preprocessor from going into infinite loop (or throwing an error because of this), you will get unexpected compiler errors, because at some point a header file will not be included when if you think it is.
You should include A.h, B.h and C.h from framework.h, and in A.h, do not include framework.h, just forward declare the classes you use from it. Or do it the other way around: include framework.h from A.h, B.h and C.h, and forward declare classes in framework.h. And, of course, put every code that would require any more detailed declarations than for example class A to the .cpp files.

Multi Including a .h File

In an .h file, I am declaring a global variable as:
#pragma data_seg(".shared")
#ifndef DEF_VARX
#define DEF_VARX
int VARX=0;
#endif /*DEF_VARX*/
#pragma data_seg()
#pragma comment(linker, "/SECTION:.shared,RWS")
However if I include this file in multiple cpp files, when I try to compile, I get " error LNK2005: "int VARX" (?VARX##3HA) already defined in Dll.obj" error. If I include in only one cpp file, no problem is encountered.
Isn't #IFNDEF.... check enough for preventing this? Am I missin something?
The reason of this behavior is, that you compile the line
int VARX=0;
into each .obj file. Thats OK for compiling, but upon linking the symbol becomes multiply defined, which is illegal. Using
extern int VARX;
in the header file, and
int VARX=0;
in one (and only one) source file resolves this problem.
I think you're supposed to forward declare the variable in the .h and later define it in its shared section in a .cpp, something like:
// in a header file
#pragma once
extern int VARX;
// in a .cpp
#pragma data_seg(".shared")
int VARX=0;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.shared,RWS")
The problem is that is that you prevent multiple inclusion of the file for a given translation unit. (for a given say cpp file)
But if several of your cpp include this VARX.H, then you'll have more than one definition for this variable.
Instead, you should only declare the variable in the .H file, but initialize it to 0 in only one location.
Yes, you're missing the extern keyword.
In your header file, use:
extern int VARX;
In a source file, actually declare space for the variable:
int VARX = 0;
ifdef prevents it for a separe object file. When the header is include in several source (cpp) files, VARX will be dedfined in all of them. Consider declaring it as extern in header file, and initialize in one cpp file.
The problem is you must be including the file in multiple compilation units. Let's say you have a.cpp and b.cpp. Both include your header file. So the compiler will compile (and pre-process) separately, so for both files, DEF_VARX is not yet defined. When you go to link the to object files together, the linker notices that there is a name collision.
As others have suggested, the solution would be to declare it 'extern', then place the actual value in a cpp file, so it only is compiled once, and linked to everything without name collisions.