VC++;中已定义错误LNK2005 我目前正在使用C++程序,我的IDE明显是VC++,我遇到了链接错误。 error LNK2005: "class Graphics TheGraphics" (?TheGraphics@@3VGraphics@@A) already defined in Game.obj

VC++;中已定义错误LNK2005 我目前正在使用C++程序,我的IDE明显是VC++,我遇到了链接错误。 error LNK2005: "class Graphics TheGraphics" (?TheGraphics@@3VGraphics@@A) already defined in Game.obj,c++,visual-c++,include,linker-errors,C++,Visual C++,Include,Linker Errors,我查看了如何修复它,但没有一个链接修复了它。所以我想我自己问你们所有人。 这些都是我的文件 Game.h Graphics.h Inc.h Main.h Game.cpp Graphics.cpp Main.cpp WndProc.cpp 在所有的头文件中,我都有 #ifndef ... #define... .. #endif 我在头文件中也有一些包含 在Inc.h中,我在#ifndef和#define之间有这个 我还创建了游戏对象和图形对象 extern Game TheGame; e

我查看了如何修复它,但没有一个链接修复了它。所以我想我自己问你们所有人。 这些都是我的文件

Game.h
Graphics.h
Inc.h
Main.h

Game.cpp
Graphics.cpp
Main.cpp
WndProc.cpp
在所有的头文件中,我都有

#ifndef ...
#define...
..
#endif
我在头文件中也有一些包含

在Inc.h中,我在#ifndef和#define之间有这个

我还创建了游戏对象和图形对象

extern Game TheGame;
extern Graphics TheGraphics
我还声明了1个函数

LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT Msg,
                         WPARAM wParam,
                         LPARAM lParam);
那是主要的

在Game.h和Graphics.h中,在#ifndef和#define之前我有这个

在Game.h中,我创建了一个名为Game的类和一个名为GAMESTATE的枚举。 在图形学方面,我上了一门课叫图形学

该类的所有.cpp文件仅包含其类头,WndProc包含Main.h

毕竟我在编译的时候得到了这个

1>Graphics.obj : error LNK2005: "class Graphics TheGraphics" (?TheGraphics@@3VGraphics@@A) already defined in Game.obj
1>Graphics.obj : error LNK2005: "class Game TheGame" (?TheGame@@3VGame@@A) already defined in Game.obj
1>Main.obj : error LNK2005: "class Graphics TheGraphics" (?TheGraphics@@3VGraphics@@A) already defined in Game.obj
1>Main.obj : error LNK2005: "class Game TheGame" (?TheGame@@3VGame@@A) already defined in Game.obj
1>WndProc.obj : error LNK2005: "class Graphics TheGraphics" (?TheGraphics@@3VGraphics@@A) already defined in Game.obj
1>WndProc.obj : error LNK2005: "class Game TheGame" (?TheGame@@3VGame@@A) already defined in Game.obj
1>F:\Games\Zombie Lypse\Release\Zombie Lypse.exe : fatal error LNK1169: one or more multiply defined symbols found

请帮帮我,我到处都找过了,我也试着自己修。还是没什么。

您在任何地方都外部化了全局变量,但在任何地方都没有定义它

extern
不定义变量。它只是告诉编译器变量是在别处定义的。所以你必须在别处定义它

你可以这样做

在头文件中

/* main.h */

MYEXTERN Game TheGame;
MYEXTERN Graphics TheGraphics
在第一个.c文件中

In main.cpp

/* MYEXTERN doesn't evaluate to anything, so var gets defined here */
#define MYEXTERN 
#include "main.h"
在其他.cpp文件中

/* MYEXTERN evaluates to extern, so var gets externed in all other CPP files */
#define MYEXTERN extern
#include "main.h"

因此,它只在一个.cpp文件中定义,并在所有其他文件中被外部化。

我在
Main.h
Main.cpp中有
Game TheGame
Graphics TheGraphics
Main.cpp中有
Game
Graphics
,这不是它的支持方式吗?
/* main.h */

MYEXTERN Game TheGame;
MYEXTERN Graphics TheGraphics
/* MYEXTERN doesn't evaluate to anything, so var gets defined here */
#define MYEXTERN 
#include "main.h"
/* MYEXTERN evaluates to extern, so var gets externed in all other CPP files */
#define MYEXTERN extern
#include "main.h"