0

我收到了这个 LNK 2005 错误,即使我已经使用正确的头文件和 cpp 文件格式创建了游戏类,据我所知。

在谷歌搜索问题一段时间后似乎是这个错误的主要原因,谁能看到我搞砸了什么?

我的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;
}
4

1 回答 1

3

包含 时game.cpp,您实际上实现了构造函数Game::Game()两次,即一次 in game.cpp(这是一个单独的翻译单元)和一次 in main.cpp(您包含构造函数实现代码)。因此,您会收到链接器错误,而不是编译器错误。

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

于 2017-08-16T06:49:46.557 回答