pad*_*ddy 37
对不起,我忽略了除了使用头文件之外的其他任何答案的请求.当你正确使用它们时,这就是标题的用途......仔细阅读:
global.h
#ifndef MY_GLOBALS_H
#define MY_GLOBALS_H
// This is a declaration of your variable, which tells the linker this value
// is found elsewhere. Anyone who wishes to use it must include global.h,
// either directly or indirectly.
extern int myglobalint;
#endif
Run Code Online (Sandbox Code Playgroud)
global.cpp
#include "global.h"
// This is the definition of your variable. It can only happen in one place.
// You must include global.h so that the compiler matches it to the correct
// one, and doesn't implicitly convert it to static.
int myglobalint = 0;
Run Code Online (Sandbox Code Playgroud)
user.cpp
// Anyone who uses the global value must include the appropriate header.
#include "global.h"
void SomeFunction()
{
// Now you can access the variable.
int temp = myglobalint;
}
Run Code Online (Sandbox Code Playgroud)
现在,当您编译和链接项目时,您必须:
使用我上面给出的语法,您既不应该编译也不应该链接错误.