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.