0
저는 C++ 프로그래밍을 처음 사용합니다. 나는 JAVA에 대해 더 잘 알고 있었다. 따라서 hackerrank를 사용하여 C++을 배우려고합니다. sepearate 엔티티의 각 프로그램을 추적하기 위해 각 프로그램이나 챌린지에 헤더 파일과 프로그램 파일 을 사용하기 시작했습니다. 그래서 저는 해커 런크 운동을하려고합니다. 입력 및 출력 (https://www.hackerrank.com/challenges/cpp-input-and-output). 그래서이 방법으로 프로그램을 구현하려고했습니다.헤더 파일에서 선언하고 파일에서 정의하는 것이 여러 정의 오류를 발생시키는 이유는 무엇입니까?
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 ;
}
이 O ne는 다음과 같은 오류를 표시합니다.
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
나는 이클립스를 사용하고 그것을 컴파일시에 오류를 던지고이 notation.BTW 뭐가 잘못 됐는지 설명해주십시오.