C++包括标头在内的多个定义
multiple definition in C++ including header
我有一个有多个文件的项目,如下所示:
//header.h
class example {...}
//variable.h
#include "header.h"
example ex;
//main.cpp
#include "variable.h"
....
//src1.cpp
#include "variable.h"
编译编译器错误时:多重定义"ex" 我不明白为什么,我想在main中使用"ex".cpp src1.cpp,我该怎么办。 谢谢
通过main.cpp
和src1.cpp
#includingvariable.h
,你已经定义了变量ex
两次。链接器(不是编译器(不会喜欢这样。
相反,请将variable.h
更改为如下所示:
extern example ex;
并把:
example ex;
在(说(src1.cpp
.是的,使用也包括警卫,但这不是这里的问题。
在 header.h 和 variable.h 中使用 include guards:
#ifndef HEADER_H
#define HEADER_H
class header {...}
#endif
相关文章:
- 在没有类定义的标头中定义成员变量
- C++包括标头在内的多个定义
- g++ 生成依赖项文件缺少用户定义的标头
- 哪个标头(cstddef,cstdio,cstdlib等)重要吗?我包括得到size_t的定义?
- 为C /CLI中的所有请求添加自定义的标头到WebBrowser控件
- 在没有多个定义的标头文件中包括一个CPP文件
- 如何检查是否定义了标头保护
- 定义从标头生成 gSOAP 文件时无法识别
- Eclipse CDT将方法定义定义到标头文件
- 程序运行时包括标头文件
- 我的C程序如何可以从C 文件调用函数,而无需包括标头
- 重新定义C 标头文件
- C2065:'class'未声明的标识符,包括标头
- 包括 Windows 标头与用户定义的类名冲突
- 模板的类型定义包括char[][] - 适用于VS2008,但不适用于gcc
- 无法使用标头保护解决包含其自身的文件(包括标头)的错误
- 包括标头和 Main.h
- 将字符串流定义为标头中的成员时发生Malloc错误
- VS 2008 - 不包括标头的某些部分
- 包括标头中的结构