Redefinition and fail to use ifned/def/endif - c++

I'm trying to make a program that follows the following UML diagram
I've divided each class into its respective header and cpp. However, while doing some tests on it I found plenty of messages of redefinition so I tried using ifndef, def and endif however as you can see in this image (my commission worker header) it seems as if I wasn't including the employee header at all
I'm using Visual Studio Code if that's relevant (Also, I doubled checked my file's name so that's not the problem)

You include the header file for the Employee Class by using the preprocessor directive #include "HEADERNAME.h/hpp"
In your case: #include "Employee.h", if your header file is called like that.
You used so called "Include/Header guards" which are used to prevent multiple header inclusions. Those should be put in the respective header file, not in other header files that include it.
Alternatively you can use #pragma once. It's non-standard, but should be widely supported.
For your strings you wanna address the namespace std as such: std::string

Related

precompiled header clang redefinition of error [duplicate]

I'm writing a library based on OpenVPN3, which is the C++ OpenVPN client implementation in header only, no cpp files. Therefore, I'm having to rely on having only one cpp file, which is the client itself, which includes a header that includes tons of other headers.
The problem is that, because of this, I cannot separate code into multiple cpp files. I'd like to be able for people to use my library, be it precompiled or compiled by them, but they cannot include the same headers on more than 1 cpp file or in the linking process there will be lots of redefinitions. There are also some static variables in the headers, for example.
If someone want to take a look at the number of things added into the 'master' header file: https://github.com/lucaszanella/libopenvpn3/blob/9b3440a736d90b671e9376d2d9e4911475e07112/src/OpenVPNClient.hpp
I know that there are some libraries like Asio that are also header-only and they're used without any problems by everyone.
Some techniques for not redefining a class or a function are to forward declare them but give no definition, but the problem here is that the person who's using my library is going to have to access its methods and everything. Is it possible to separate my methods from the ones used by my library on the headers?
you can put #pragma once on the most top of the file to avoid the same class clash due to being defined twice
If you do not want to duplicate import you can like tadman said:
at the top of the file
#ifndef HEADER
#define HEADER
//code goes here
#endif

How to avoid class/variable redefinition in C++ header only libraries

I'm writing a library based on OpenVPN3, which is the C++ OpenVPN client implementation in header only, no cpp files. Therefore, I'm having to rely on having only one cpp file, which is the client itself, which includes a header that includes tons of other headers.
The problem is that, because of this, I cannot separate code into multiple cpp files. I'd like to be able for people to use my library, be it precompiled or compiled by them, but they cannot include the same headers on more than 1 cpp file or in the linking process there will be lots of redefinitions. There are also some static variables in the headers, for example.
If someone want to take a look at the number of things added into the 'master' header file: https://github.com/lucaszanella/libopenvpn3/blob/9b3440a736d90b671e9376d2d9e4911475e07112/src/OpenVPNClient.hpp
I know that there are some libraries like Asio that are also header-only and they're used without any problems by everyone.
Some techniques for not redefining a class or a function are to forward declare them but give no definition, but the problem here is that the person who's using my library is going to have to access its methods and everything. Is it possible to separate my methods from the ones used by my library on the headers?
you can put #pragma once on the most top of the file to avoid the same class clash due to being defined twice
If you do not want to duplicate import you can like tadman said:
at the top of the file
#ifndef HEADER
#define HEADER
//code goes here
#endif

Use of header files consisting only on #include directives?

I am new to C++ and I'm currently reading some code that has multiple header files of the form:
#include <some standard library>
#include <some standard library>
...
#include <some third party library header>
#include <some third party library header>
...
#include <some internal header files>
#include <some internal header files>
...
and nothing else. I don't really understand the use of these files, or why they are structured this way, since I thought that header files carried class declarations that basically consist on the interface that other code uses to access an object file. I think the programmer may have wanted to "package" all the headers inside one big header and just include that. Is this a good idea or is it better to avoid these "include only" headers?
The idea is precompiled headers.
Instead of including all headers in all compilation units, only one file is included which, after the first parse it's cached. Then, compilation of the rest of the units for the current or the next builds is faster.
Hence the big PCH files you see in Visual Studio, for example. Typical setup is stdafx.h which is included in all compilation units, where the first one produces the precompiled header, the others reuse it.

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.

Header files repeatedly used in different header files in my static library

Since I got multiple classes and utility-functions I reuse on a regular basis, I started to create a static library for those parts.
Until now, most of those header files with class and function declarations #include the same "global" header file which itself #includes other header files (like <string> <windows.h> etc.).
As for me, It feels annoying to force a user to include more than one header file when only "actively" using the one which got the classes.
So I vanquished the global header file and included all the necessary headers into my own headers.
This worked in some way, but I encountered some problems regarding Winsock:
You need to #define WIN32_LEAN_AND_MEAN and ensure that the user won´t #include <windows.h> before those headers of mine, or multiple redefinitions will occur :/.
That´s why I wanted to ask You how You would do this? Or what approches You can think of?
I think you only have the option of defining a general header file that all other header files include and that defines WIN32_LEAN_AND_MEAN.
Otherwise you could instruct the user to include such definition in his own files, but his would be error prone.
As to inclusion of <windows.h> I think that it will not be included multiple times, since it has "include guards".