Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring variables in header files C++ [duplicate]

Tags:

I'm trying to create a simple program that takes input from the user in C++ using good programming practices. It consists of Input.hpp, Input.cpp and main.cpp. I keep getting a multiple definition error even though I am using ifndef to prevent that.

Input.hpp

#ifndef Input_HPP #define Input_HPP  #include <string> #include <vector> using namespace std;  vector<string> Get_Input(); vector<string> input_array; string starting_position; int input_number;  #endif 

Input.cpp

#include <iostream> #include <cmath> #include <string> #include <vector>  #include "Input.hpp"  using namespace std;  vector<string> Get_Input() {     cin>>starting_position;     cin>>input_number;     for (int i = 0; i < input_number; i++)     {         cin>>input_array[i];      }     cout<<"Done";      return input_array; } 

main.cpp

#include "Input.hpp" #include <iostream> using namespace std;  int main() {      Get_Input();     return 0; } 

When I remove the variable declarations from the header file and put them in the cpp file but keep the function declaration in the header file the program builds without errors. It is my understanding that variables and functions can be declared in header files. Can someone please explain to me what I am missing?

Thank you.

like image 236
Kareem Aboughazala Avatar asked Aug 14 '16 11:08

Kareem Aboughazala


People also ask

Can you declare variables in header file C?

Yes. Although this is not necessarily recommended, it can be easily accomplished with the correct set of macros and a header file. Typically, you should declare variables in C files and create extern definitions for them in header files.

What happens if we include a header file twice in C?

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.

What will happen if the variable is declared in the .h file?

If you declare variables in a . H file, then that is the same as declaring them in your . C file, outside the functions, so as global variables.

How do you declare a variable in a header file?

The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.


1 Answers

The Header file isn't very smart, it just tells the pre-processor to take the whole header and put it instead of the include line.

if you do that, you can see the variable is declared twice.

To solve the issue, you should declare the variables in one of your cpp files and use extern in the headers.

like in input.cpp:

int input_number; 

and in input.hpp:

extern int input_number; 
like image 121
SHR Avatar answered Oct 09 '22 14:10

SHR