C++ Header Guards - c++

Suppose I have the following header file:
#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <string>
class TestClass
{
public:
TestClass();
std::string test();
};
#endif // TESTCLASS_H
Do I have to put a guard around #include <string> as well? If not, what if main.cpp also has #include <string>?

No, because the string header file has got its own include guards (as do the header files of all sensible libraries).

Not necesarry, the c++ standard libs have their own Guards.

Related

How to Have Preprocessor Statements in Header File Depend on Which C++ File is Including It

How can I exclude certain #include statements in my .h file depending on which .cpp is including the .h file?
Example:
main.cpp file
<tell the header.h file that main.cpp is including it>
#include "header.h"
other.cpp file
<tell the header.h file that other.cpp is including it>
#include "header.h"
header.h file
<if called by main.cpp>
#include "some_file_which_fails_when_used_with_OTHER_CPP.h"
<end if>
The typical way to do this is to expect source files to #define a macro prior to including the header:
// the_header.h
#ifndef MY_HEADER_H_INCLUDED
#define MY_HEADER_H_INCLUDED
#ifdef MY_PROJECT_MAIN
// ...
#else
// ...
#endif
#endif
In files using the header's "default" behavior
// some_code.cpp
// Just use the header
#include "the_header.h"
In files using the header's activated behavior:
// main.cpp
// Make sure this is BEFORE the #include
#define MY_PROJECT_MAIN
#include "the_header.h"

Inclusion vector module conflict c++

I have a problem with the inclusion of the vector module. It seems to there is a conflit with others modules. Here is the structure :
In the simulation.h :
#pragma once
#ifndef SIMULATION
#define SIMULATION
#include <ostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <cstdlib>
// #include <vector>
#include "File.h"
...
void afficherResultat(std::vector<Client> sortie);
...
#endif
And the File.h file :
#pragma once
#ifndef FILE
#define FILE
#include <vector>
class File {
...
std::vector<Client> l;
...
};
#endif
And I get 108 errors starting with : C4091 warning and C4430, C2065, C4229 errors...
Some people spotlight the order of the inclusions. Any Ideas ?
You are defining a macro for an identifier which is part of the standard library:
#define FILE
(see https://en.cppreference.com/w/cpp/io/c#Types for what FILE is).
Doing so is forbidden and will cause very weird errors.
Instead use names which are as unique as possible as include guards, e.g. INCLUDE_GUARD_FILE_H.
If you have an include guard there is also no need for #pragma once which is a non-standard way of solving the double inclusion problem that the include guard is also supposed to prevent.
Additionally you have not declared Client in File.h. Probably some #include for the header file defining Client is missing.

Global variable error C++

I'm having an error I don't know how to fix in my large Operating Systems homework. The error I'm getting is "42 duplicate symbols for architecture x86_64". I presume this is to do with my global variables file "global.h". I have 3 global variables I use and "global.h" is included in an abstract class called "PageReplacementAlgorithm.cpp". I have around 6 classes that are derived from the PageReplacementAlgorithm class and they utilize these global variables. I think the problem comes in when I include all these derived classes in my "main.cpp" as I need to make new instances of them. How can I fix the implementation of the global variables?
Global.h
#include "PageTableEntry.h"
using namespace std;
#ifndef Global_H
#define Global_H
extern PageTableEntry pageTable[64];
extern int* frameTable;
extern int framesCount;
#endif
PageReplacementAlgorithm.h
#include "Global.h"
using namespace std;
#ifndef PageReplacementAlgorithm_H
#define PageReplacementAlgorithm_H
class PageReplacementAlgorithm {
public:
virtual int selectFrame(PageTableEntry &p) = 0;
};
#endif
Example Derived Class (FIFO)
include "PageReplacementAlgorithm.h"
using namespace std;
#ifndef FIFO_H
#define FIFO_H
class FIFO : public PageReplacementAlgorithm {
public:
FIFO();
int selectFrame(PageTableEntry &p);
private:
int entries;
};
#endif
Main.cpp
#include "Aging.cpp"
#include "Clock.cpp"
#include "FIFO.cpp"
#include "MMU.cpp"
#include "NRU.cpp"
#include "Random.cpp"
#include "SecondChance.cpp"
Why do you include all cpp files in main.cpp? I think they contain same includes, right? Even you have the guards there, you do additional includes before that guards and that is probably the source of problems. The main.cpp could contain just main() function and import headers of your classes, there is no need to include cpp.
Also, you can modify your header files to look like this (for sake of extreme safety):
#ifndef PageReplacementAlgorithm_H
#define PageReplacementAlgorithm_H
#include "Global.h"
using namespace std;
...
#endif
I recommend you to look at answer C++ #include guards
If you get rid of #include "(anything).cpp, things should work much better. When you build the project, or run the compiler e.g. g++ main.cpp foo.cpp, that's when those .cpp files get built and linked into your program.

C++ Do I have to include standard libraries for every source file?

I'm a bit confused at the moment because I'm planning to include multiple source and header files for the first time in one of my projects.
So I'm wondering if this would be the right approach?
Do I have to include the string header in every source file that uses it directly?
And what about the "stdafx.hpp" header that Visual C++ wants me to include?
Would that be the way to go?
main.cpp
#include "stdafx.hpp"
#include <string> //?
#include <stringLib1.h>
#include <stringLib2.h>
using std::string;
//use a windows.h function here
//use a stringLib1 function here
//use a stringLib2 function here
stringLib1.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass1
{
public:
string GetStringBack1(string myString);
};
stringLib1.cpp
#include "stdafx.hpp"
string uselessClass1::GetStringBack1(string myString) {
return myString;
}
stringLib2.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass2
{
public:
string GetStringBack2(string myString);
};
stringLib2.cpp
#include "stdafx.hpp"
string uselessClass2::GetStringBack2(string myString) {
return myString;
}
A good practice is usually to include only what your code uses in every file. That reduces dependencies on other headers and, on large projects, reduce compilation times (and also helps finding out what depends on what)
Use include guards in your header files
Don't import everything by polluting the global namespace, e.g.
using namespace std;
but rather qualify what you intend to use when you need it
You don't need stdafx.h in your project unless you're using precompiled headers. You can control this behavior in the VS project properties (C/C++ -> Precompiled Headers -> Precompiled Header)
The stdafx.h header is needed if precompiled header is enabled in VS. (Read this one)
You only need to include the stdafx.h in your .cpp files as the first include.
Regarding the header and cpp files (which come in pairs), include things necessary for the declaration in the header, and include everything else (necessary for the definition) in the cpp. Also include the corresponding header in its cpp pair too. And use include guards.
myclass.h
#ifndef MYCLASS_H // This is the include guard macro
#define MYCLASS_H
#include <string>
using namespace std;
class MyClass {
private:
string myString;
public:
MyClass(string s) {myString = s;}
string getString(void) {return myString;}
void generate();
}
myclass.cpp
#include <stdafx.h> // VS: Precompiled Header
// Include the header pair
#include "myclass.h" // With this one <string> gets included too
// Other stuff used internally
#include <vector>
#include <iostream>
void MyClass::generate() {
vector<string> myRandomStrings;
...
cout << "Done\n";
}
#endif
Then in main(...), you can just include myclass.h and call generate() function.
The stdafx include should be at the top of every .cpp file and it should NOT be in .h files.
You could put #include < string > in stdafx.h if you don't want to put it in every other file.
I suppose that you must be having your own header files also which might be requiring in other cpp files and header files. Like the one you gave
#include <stringLib1.h>
#include <stringLib2.h>
In my opinion, its better to create one common header file in which you include all the common library header files and your project header file. This file then you can include in all the other cpp files and header file. And it will be better to use header guards also.
So, considering a common header file "includes.h".
#ifndef INCLUDES_H
#define INCLUDES_H
#include <string>
#include <stringLib1.h>
#include <stringLib2.h>
/***Header files***/
#endif //INCLUDES_H
This is now your common header file. This you can include in all your project files.

vector of structs giving error "not declared in this scope"

I have a struct declared as follows:
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <iostream>
#include <string>
#include <vector>
#include "playlistitem.h"
#include "song.h"
#include "time.h"
struct Playlist {
std::vector<Song> songs;
Time cdTotalTime;
int totalTime;
};
and struct Song declared in another file:
#ifndef SONG_H
#define SONG_H
#include "playlist.h"
#include "time.h"
struct Song {
std::string title;
std::string artist;
std::string album;
int track;
Time length;
};
I have both struct definitions in headers, and both are #included as they should be.
When I compile I get an error at
std:vector<Song> songs;
error 'Song' was not declared in this scope
What am I missing?
playlist.h includes song.h
song.h should NOT include playlist.h
Header guards prevent infinite recursion, they don't fix circular dependencies.
Currently song.h does include playlist.h. Then when playlist.h includes song.h, nothing happens (because of the header guard), and Song is not defined. So playlist.h produces errors.
Not only your main file, but the file where Playlist is declared should also #include the file where Song is in.
The definition of Song has to come before its used in the definition of Playlist. Since they are in different headers, you should make sure that the header for Playlist includes that of Song, and both have proper header guards.
Your headers include each other in circular fashion. This is useless and unnecessary. Why is your song.h including playlist.h? Remove #include "playlist.h" from song.h and that should fix the error.
You could prototype Song in your header by putting struct Song; and just include the header in your .c/.cpp file. This has the bonus of faster compile times! :D
Recursive includes work fine with include guards, as long as you arrange them properly. I always try and include the least amount of headers in .h files, leaving them for the source files.
Also, I don't see a #endif on your code. Right now I'm assuming your code actually has it ;)