I am trying to learn the C++ language (so kindly bear with my noobness). On following the tutorial in several books I decided to experiment with headers
I have a file named Untitled2.cpp which contains
#include <iostream>
#include "findaverage.h"
#include <string>
using namespace std;
int main(){
cout<<find_average();
}
A header file named findaverage.h which contain
#ifndef FINDAVERAGE_H
#define FINDAVERAGE_H
int find_average();
double first_no;
double second_no;
#endif
and a findaverage.cpp file which contain
#include "findaverage.h"
#include <iostream>
using namespace std;
int find_average(){
std::cout << "Enter a number"<<endl;
std::cin>>first_no;
std::cout<<"Enter another number"<<endl;
std::cin>>second_no;
return (first_no+second_no)/2;
};
when i compile the program in Code::Blocks, I get the following error
||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|
obj/Debug/Untitled2.o||In function `main':|
/root/Untitled2.cpp|7|multiple definition of `second_no'|
obj/Debug/findaverage.o:/root/findaverage.cpp|5|first defined here|
||error: ld returned 1 exit status|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I am trying to follow the an approach similar to this picture
Question: how can I successfully resolve the multiple definition issue in my experimental code?