Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用C+的问题+;在另一个文件中定义的对象_C++_Class_Linker - Fatal编程技术网

C++ 使用C+的问题+;在另一个文件中定义的对象

C++ 使用C+的问题+;在另一个文件中定义的对象,c++,class,linker,C++,Class,Linker,在mymain.cpp中,我有以下内容: #包括“main.h” int main(){ ... SDL_窗口*gWindow=NULL; SDL_渲染器*gRenderer=NULL; if(initProg(gWindow,grender)!=0){ 返回1; } 脱欧(SDL_退出); 玩家主要玩家(格伦德勒); ... } 其中Player是在Player.h上定义的类,该类包含在main.h上,如下所示: 。。。 #包括“player.h” ... 外部玩家主玩家; ... 无效ha

在my
main.cpp
中,我有以下内容:

#包括“main.h”
int main(){
...
SDL_窗口*gWindow=NULL;
SDL_渲染器*gRenderer=NULL;
if(initProg(gWindow,grender)!=0){
返回1;
}
脱欧(SDL_退出);
玩家主要玩家(格伦德勒);
...
}
其中Player是在
Player.h
上定义的类,该类包含在
main.h
上,如下所示:

。。。
#包括“player.h”
...
外部玩家主玩家;
...
无效handlePlayerInput(SDL_事件);
...
现在,在代码的后面,我需要调用
handlePlayerInput()
,我已经将它放在
input.cpp
文件中来组织我的代码。我在访问
mainPlayer
对象时遇到问题,但如下代码所示

#包括“main.h”
...
案例SDLK\U d:
mainPlayer.movePlayer(向量2(1,0));
打破
...
返回:

undefined reference to 'mainPlayer'
collect2: error: ld return 1 exit status

我正在使用CMake编译所有代码,其中我将所有与游戏相关的源文件(main.cpp、player.cpp、input.cpp等)添加到库中,并在编译可执行文件时将该库与其他与游戏无关的代码链接起来。

是否将main.cpp中的mainPlayer变量声明为全局变量?也就是说,它是否在任何函数之外?main中的mainPlayer.h接受参数@Ian4264,我在main.h中将其声明为
extern
,并在
main()
函数中对其进行定义,因为我需要将更大的指针传递给它。您需要在全局范围内对其进行定义,以使其可供其他模块使用。