Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图将我的代码分成标题和 cpp 我为我拥有的每个类创建了一个 .h 和 .cpp 但我不知道将函数(不在类中)和 我尝试放置的全局变量放在哪里它们在 main 中,但没有 用
在.h中:
extern int myGlobal; int myFunction(int arg);
在.cpp 文件之一中:
int myGlobal; int myFunction(int arg) { return arg + 5; }
您可以将内容拆分到多个 .cpp 文件中,只是不要在多个地方定义任何内容。如果您将定义放在 .h 文件中,并多次包含它,您会得到多个(冲突的)定义。