软件
- 编辑器:CLion
问题描述
今天学习C++按照老师课程所讲内容操作,在我本地抛出如下提示:
In file included from f:\code\c++\header\line.h:1:0,
from F:\Code\c++\main.cpp:5:
f:\code\c++\header\Coordinate.h:3:7: error: redefinition of 'class Coordinate'
class Coordinate {
^~~~~~~~~~
原因
在该类定义的头文件(.h)中没有使用 #ifndef,#define 和 #endif 来进行保护,也就是缺少了防止类被多次包含的宏
- 我使用IDE编辑器(CLion),新建头文件,会在头文件中加入如下代码:
#ifndef C_TEST_H
#define C_TEST_H
#endif //C_TEST_H
- 我看到老师编辑器中没有内容,以为不重要就删除了,结果然我意想不到…
解决办法
将你头文件中的代码放到如下位置:
#ifndef C_TEST_H
#define C_TEST_H
# 这里放你写的代码
#endif //C_TEST_H
注意:如果你的头文件名称为Test.h , #ifndef 、#define
后面的代码为 C_TEST_H