저는 "Accelerated C++"라는 책으로 제 1 언어를 배우는 학생 프로그래머입니다. 필자는 저자가 헤더 파일과 함수를 포함하는 소스 파일을 설명하는 시점에 있습니다. 책이 설명을 제공하는 연습에서 작성자는 함수에 대한 정의가 들어있는 헤더 파일을 가지고 있지만 소스 파일에도 함수에 대한 정의가 있으므로 중복 된 것처럼 보입니다. C++을 프로그래밍 할 때이 경우 헤더 파일의 요점은 무엇입니까?C 헤드 파일과 소스 파일
예 : 헤더 파일.
#ifndef MEDIAN_H
#define MEDIAN_H
#include <vector>
double median(std::vector<double>);
#endif // MEDIAN_H
는 다음 기능을 포함하는 소스 파일 등급의 중간 값을 결정 :
median.h는 소스가 이미 정의vector<double> vec
책이있다하더라도 중간 기능의 소스 파일로 복사
// source file for the `median' function
#include <algorithm> // to get the declaration of `sort'
#include <stdexcept> // to get the declaration of `domain_error'
#include <vector> // to get the declaration of `vector'
using std::domain_error; using std::sort; using std::vector;
#include "median.h"
// compute the median of a `vector<double>'
// note that calling this function copies the entire argument `vector'
double median(vector<double> vec)
{
#ifdef _MSC_VER
typedef std::vector<double>::size_type vec_sz;
#else
typedef vector<double>::size_type vec_sz;
#endif
vec_sz size = vec.size();
if (size == 0)
throw domain_error("median of an empty vector");
sort(vec.begin(), vec.end());
vec_sz mid = size/2;
return size % 2 == 0 ? (vec[mid] + vec[mid-1])/2 : vec[mid];
}
무해하고 실제로는 좋은 생각이라고 설명합니다. 그러나 나는 그런 중복성에 대한 이유를 더 잘 이해하고 싶다. 어떤 설명이 좋을 것입니다!
헤더에는 일반적으로 ** 선언 **이 있습니다. 구현에는 완전한 정의가 있습니다. – chris
여러 파일이있는 프로젝트 작업을 시작할 때 필요성을 알 수 있습니다. – jman
http://skackoverflow.com/questions/1305947/why-does-c-need-a-separate-header-file – jman