I am new to programming in c++. I had some better knowledge on JAVA. So using hackerrank I am trying to learn C++. To keep track of each program a sepearate entity I started using a Header File and Program file for each program or challenge. So I am trying to do the hackerrank exercise Input and Output (https://www.hackerrank.com/challenges/cpp-input-and-output). So I tried to implement my program in this manner;
InputAndOuput.h
#ifndef INPUTANDOUTPUT_H_
#define INPUTANDOUTPUT_H_
int arr[3];
int m;
int InputAndOutput();
#endif
InputAndOutput.cpp
#include "InputAndOutput.h"
#include<iostream>
#include<cmath>
#include<cstdio>
int InputAndOutput(){
int arr[3];
for(int i1 = 0; i1 < 3 ; i1++)
std::cin >> arr[i1];
for(int i = 0; i < 3 ; i++)
m = m + arr[i];
return m;
}
main.cpp
#include<iostream>
//#include<day1DataTypes>
#include<cmath>
#include<cstdio>
//#include "day1DataTypes.h"
#include "InputAndOutput.h"
int main()
{
int k = InputAndOutput(); \\the error persists even the whole block is commented
std::cout << k << std::endl ;
}
This one is giving the following errors;
Description Resource Path Location Type
first defined here Hackerrank line 6 C/C++ Problem
first defined here Hackerrank line 8 C/C++ Problem
make: *** [Hackerrank] Error 1 Hackerrank C/C++ Problem
multiple definition of `arr' Main.cpp /Hackerrank line 9 C/C++ Problem
multiple definition of `m' Main.cpp /Hackerrank line 12 C/C++ Problem
Please explain me what's wrong with this notation.BTW I am using eclipse and it's throwing error at compile time.