为什么我不需要包括main.cpp
Why do I not need to include main.cpp?
在小示例中,needsExtern.cpp需要global::bar
的定义。cpp通常会包含带有定义的文件(在本例中为main.cpp)。但是,由于该文件是main.cpp,因此不需要它。
为什么needsExtern.cpp不需要包含main.cpp?
needsExtern.h
struct NeedsExtern
{
NeedsExtern();
};
needsExtern.cpp
#include "needsExtern.h"
#include <iostream>
namespace global
{
extern const int bar;
}
NeedsExtern::NeedsExtern()
{
std::cout << global::bar << "n";
}
main.cpp
#include "needsExtern.h"
namespace global
{
extern const int bar{26};
}
void main()
{
NeedsExtern ne;
}
这正是extern
被发明的原因:编译器只是假设变量是在项目的其他地方定义的。
相关文章:
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- 在 main.cpp 的上下文中找不到目录(带有 CMake 的快板)
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- main.cpp(11):错误 C2059:语法错误:"字符串"
- 访问除main.cpp之外的.cpp文件中的QMLEngine/rootObject属性
- 生成main.c和main.cpp的单个Makefile
- C 函数未从 main.cpp C++ 调用
- 代码块始终在项目中运行 main.cpp
- main.cpp 如何知道包含标头的源文件?
- 如何使用 c++ 从另一个 cpp 文件中的 main.cpp 调用实例化类
- 当我有多个类时,我应该以线性样式包含它们还是只包含在 main.cpp 中?
- main.cpp从 .h 文件调用公共虚拟方法
- main.cpp:(.text+0x8ea):在C++中未定义的引用
- \main.cpp|103|error: 与"operator<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream<char&g
- 我运行sudo g++ main.cpp,但它没有运行程序
- 在 main.cpp 以外的文件中包含标头时出现链接器错误
- main.cpp:1:10:致命错误:opencv2/highgui.hp:没有这样的文件或目录
- main.cpp中的错误:函数库中的函数"not declared in this scope",但实际上是
- C++模板:无法从 main.cpp 调用我的函数
- 希望修复 main.cpp 与"std::istream&<< int&"运算符不匹配