C++多个头文件重复定义变量问题

来源:互联网 发布:picsart素材软件 编辑:程序博客网 时间:2024/04/30 15:11

验证步骤

head_file1.h的内容如下:

#ifndef _HEAD_FILE1_H
#define _HEAD_FILE1_H

#define MAX 100

#endif/*** _HEAD_FILE1_H*/


head_file2.h的内容如下:

#ifndef _HEAD_FILE2_H
#define _HEAD_FILE2_H

#define MAX 200

#endif/*** _HEAD_FILE2_H*/


test_headfile.cpp的内容如下:

#include<iostream>
#include"head_file1.h"
#include"head_file2.h"
using namespace std;

int main()
{
        cout<<"Max is "<< MAX <<".\n"<<endl;
        return 0;
}

编译test_headfile.cpp:g++ -o test_headfile test_headfile.cpp

运行test_headfile结果如下:

Max is 200.

结论

.cpp文件包含的多个头文件中有相同的定义时,以包含的最后一个头文件为准。
0 0
原创粉丝点击