c++ - Why does Declaring in header file and defining in file gives multiple definition error? -
i new programming in c++. had better knowledge on java. using hackerrank trying learn c++. keep track of each program sepearate entity started using header file , program file each program or challenge. trying hackerrank exercise input , output (https://www.hackerrank.com/challenges/cpp-input-and-output). tried implement program in 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 = 0; < 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 whole block commented std::cout << k << std::endl ; }
this 1 giving 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 notation.btw using eclipse , it's throwing error @ compile time.
to explain simplest problem first, let's take @ "int arr[3];"
for variable declaration, declared , implemented in inputandoutput.h header.
both main.cpp , inputandoutput.cpp include header file, implementing variable twice.
to declare variable, can used in other files, use:
inputandoutput.h
extern int arr[3]; extern int m;
inputandoutput.cpp
int arr[3]; int m;
this tells compiler there 2 variables, arr , m, being declared in .h file, implemented in external file, using extern keyword.
please note, code posted in question merely c within c++ files.
in c++, discouraged use global variables storing data.
so, if remove global variables , use c++ stl containers, have following:
inputandoutput.h
#include <array> int32_t inputandoutput(std::array<int32_t, 3>& arr, int32_t& m);
inputandoutput.cpp
int32_t inputandoutput(std::array<int32_t, 3>& arr, int32_t& m) { for(auto i1 = 0; i1 < 3 ; i1++) std::cin >> arr[i1]; for(auto = 0; < 3 ; i++) m = m + arr[i]; return m; }
main.cpp
int main() { auto arr = std::array<int32_t, 3>{0,0,0}; auto m = 0; const auto k = inputandoutput(arr, m); std::cout << k << std::endl ; }
now, should take care of of question, however, don't see in original code how getting input std::cin since don't prompt user input... , leads bug.
since learning c++, should learning modern c++ , not c++98.
i recommend read on https://github.com/isocpp/cppcoreguidelines
and, check out herb sutter's website, regard almost-always-auto @ https://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/
Comments
Post a Comment