C ++中的头文件应该怎么处理? [英] What Should go in my Header File in C++?

查看:208
本文介绍了C ++中的头文件应该怎么处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始我的第一个C ++程序,我几乎只是学习,因为我去。程序应该有3个不同的文件,一个头文件(prog1.h),一个prog1.cpp文件(不确定这一个的正确的术语)和一个测试文件包括main:测试我们的程序(prog1_test.cpp )。

I'm just getting started on my first ever C++ program and am pretty much just learning as I go. The program is supposed to have 3 different files, a header file (prog1.h), a prog1.cpp file (not sure the right terminology for this one) and a test file that includes main: to test our program (prog1_test.cpp).

不要求任何帮助(但是,我相信我会发布另一个问题,一旦我进入它),但认为你需要知道该程序应该做什么,以了解我的问题。我们的程序应该从一个文件中读取一些数字,并将这些数字放入一个二维数组。然后应该为每个数字值分配字符,并打印当前数组和用字符创建的图片。然后它将通过数组,确保每个数字的值与它的邻居的值不超过1,如果是,程序将替换该数字的邻域值的平均值。然后程序将打印出这个校正的数组和用分配给校正数组的字符创建的图像。

Not asking for help on any of this (yet, I'm sure I'll be posting another question once I get into it), but figured you'd need to know what the program is supposed to do in order to understand my question. Our program is supposed to read in some numbers from a file and put these numbers into a 2D array. It's then supposed to assign characters to each number value and print the current array and the "picture" created with the characters. Then it will go through the array, making sure that each number doesn't differ in value from it's neighbors by more than 1, if it does, the program is to replace that number with the average of its neighboring values. The program will then print out this corrected array and "picture" created with the characters assigned to the corrected array.

我知道头文件应该包含跨多个文件使用的代码,但是我无法确定我的程序的哪些部分需要进入这个文件。例如,我的代码打开,读取和关闭数据文件是否包括在这里,还是没有,因为文件只被打开,读取和关闭一次?

I know that header files are supposed to include code that will be used across multiple files, but am having trouble figuring out what parts of my program need to go in this file. For example, would my code to open, read, and close the data file be included here, or no since the file is only being opened, read, and closed once?

推荐答案

头文件包含函数和类声明。这些简单地声明函数的名称,它的返回类型和它的参数列表。 .cpp文件包含这些函数的定义 - 即实际实现。如果 #include 在其他文件中,头文件对程序的其余部分可见,但实现细节隐藏在.cpp文件中。

Header files contain function and class declarations. These simply declare the name of the function, its return type, and its argument list. The .cpp file contains the definitions of these functions -- i.e. the actual implementation. The header file is visible to the rest of the program if you #include it in other files, but the implementation details are hidden in the .cpp file.

在.cpp文件中声明/定义的不在.h文件中的任何内容对程序的其余部分不可见,因此您可以定义内部变量,辅助函数等.cpp文件和那些实现细节将不可见。在我的例子中,一个例子是 foo()

Anything declared/defined in the .cpp file that is not in the .h file is not visible to the rest of the program, so you can define internal variables, helper functions, etc. in the .cpp file and those implementation details will not be visible. An example of this is foo() in my example below.

你的程序的一个非常粗略的草图像这样:

A very rough sketch of your program would look like this:

在prog1.h中:

#include <iostream> // and whatever other libraries you need to include

#define ARRAY_SIZE 100 // and other defines

// Function declarations

// Read number from file, return int
void read_number(int array[ARRAY_SIZE][ARRAY_SIZE]);

char assign_char(int n);

void print_array(int array[ARRAY_SIZE][ARRAY_SIZE]);

void neighbor_check(int array[ARRAY_SIZE][ARRAY_SIZE]);

在prog1.cpp中:

In prog1.cpp:

// included headers, defines, and functions declared in prog1.h are visible
#include "prog1.h"

void read_number(int array[ARRAY_SIZE][ARRAY_SIZE]) {
    // implementation here
}

char assign_char(int n) {
    char c;
    // implementation here
    return c;

}

void print_array(int array[ARRAY_SIZE][ARRAY_SIZE]) {
    // implementation here
}

void neighbor_check(int array[ARRAY_SIZE][ARRAY_SIZE]) {
    // implementation here
}

// not visible to anything that #includes prog1.h
// since it is not declared in prog1.h
void foo() {
    // implementation here
}

在prog1_test.cpp中:

In prog1_test.cpp:

// included headers, defines, and functions declared in prog1.h are visible
#include "prog1.h"
// any other includes needed by main()

int main() {
   int array[ARRAY_SIZE][ARRAY_SIZE];

   read_number(array);

   for (int i = 0; i < ARRAY_SIZE; i++) {
       for (int j = 0; j < ARRAY_SIZE; j++) {
           assign_char(array[i][j]);
       }
   }

   neighbor_check(array);

   print_array(array);

   return 0;
}

这篇关于C ++中的头文件应该怎么处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆