2017-08-16 69 views
-2

林已经定义得到这个LNK 2005错误,即使我已经装箱使用正确的头文件和CPP文件格式尽可能IM知道游戏类。C++ LNK 2005错误“的.OBJ

哪个google搜索后一会儿的问题似乎是这种错误的主要原因,任何人都可以看到什么是我搞砸了

我game.h文件如下:?

#pragma once 

class Game 
{ 
public: 


//Variables 
char grid[9][8] = { { '#','#','#','#','#','#','#','#' }, 
        { '#','G',' ','D','#',' ',' ','#' } , 
        { '#',' ',' ',' ','#',' ',' ','#' } , 
        { '#','#','#',' ','#',' ','D','#' } , 
        { '#',' ',' ',' ','#',' ',' ','#' } , 
        { '#',' ','#','#','#','#',' ','#' } , 
        { '#',' ',' ',' ',' ',' ',' ','#' } , 
        { '#','#','P','#','#','#','#','#' } , 
        { '#','#','#','#','#','#','#','#' } }; 
int width, height; 
int pX; 
int pY; 


char direction; 
bool west; 
bool north; 
bool south; 
bool east; 
int quit; 

Game(); 
}; 

我game.cpp文件:

#include "stdafx.h" 
#include <String> 
#include <iostream> 
#include "Game.h" 

using namespace std; 

//constructoer 
Game::Game() 
{ 
    width = 8, height = 8; 
pX = 2; 
pY = 7; 


west = false; 
north = true; 
south = false; 
east = false; 
quit = 0; 
} 

我主要是从字面上就在此刻

主要创建对象的实例:

#include "stdafx.h" 
#include <String> 
#include <iostream> 
#include "Game.cpp" 
#include "Game.h" 


using namespace std; 


int main() 
{ 
    Game g; 

    return 0; 
} 
+1

你的main()函数在哪里? –

+1

@theRoadToCodeOverload不要在注释中放置代码,但是请编辑您的问题并将该代码放在_there_处。 –

+1

@theRoadToCodeOverload A [MCVE]和逐字错误文本请。 – user0042

回答

1

当包括game.cpp,实际实现构造器Game::Game()两次,game.cpp即一个时间(这是一个单独的翻译单元)和main.cpp(包含构造函数实现代码的地方)中的一次。 因此,你会得到一个链接器错误,而不是编译器错误。

要解决此问题,请删除#include "game.cpp"