Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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++ 将头文件和.cpp文件包含到主.cpp文件LNK2005中对象中已定义错误_C++_Visual Studio 2012 - Fatal编程技术网

C++ 将头文件和.cpp文件包含到主.cpp文件LNK2005中对象中已定义错误

C++ 将头文件和.cpp文件包含到主.cpp文件LNK2005中对象中已定义错误,c++,visual-studio-2012,C++,Visual Studio 2012,我在头文件和cpp文件上挣扎了一段时间。我认为这与我的标题和cpp include语句有关,但我不知道是什么。我想把这个记下来,这样我就能进步了。我编译了大约25个错误,看起来像这样: 1> Source.cpp 1>Source.obj : error LNK2005: "public: __thiscall grid::grid(int,int)" (?? 0grid@@QAE@HH@Z) already defined in grid.obj 1>Source.obj

我在头文件和cpp文件上挣扎了一段时间。我认为这与我的标题和cpp include语句有关,但我不知道是什么。我想把这个记下来,这样我就能进步了。我编译了大约25个错误,看起来像这样:

1>  Source.cpp
1>Source.obj : error LNK2005: "public: __thiscall grid::grid(int,int)" (?? 0grid@@QAE@HH@Z) already defined in grid.obj
1>Source.obj : error LNK2005: "public: __thiscall grid::grid(int,int,int,int,int)" (??0grid@@QAE@HHHHH@Z) already defined in grid.obj
1>Source.obj : error LNK2005: "public: __thiscall grid::grid(void)" (??0grid@@QAE@XZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: void __thiscall grid::block(int,int)" (?block@grid@@QAEXHH@Z) already defined in grid.obj
1>Source.obj : error LNK2005: "private: void __thiscall grid::checkForBlock(int,int)" (?checkForBlock@grid@@AAEXHH@Z) already defined in grid.obj
1>Source.obj : error LNK2005: "public: int __thiscall grid::column(void)const " (?column@grid@@QBEHXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: void __thiscall grid::display(void)const " (?display@grid@@QBEXXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: bool __thiscall grid::frontIsClear(void)const " (?frontIsClear@grid@@QBE_NXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: void __thiscall grid::move(int)" (?move@grid@@QAEXH@Z) already defined in grid.obj
1>Source.obj : error LNK2005: "public: int __thiscall grid::nColumns(void)const " (?nColumns@grid@@QBEHXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: int __thiscall grid::nRows(void)const " (?nRows@grid@@QBEHXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: void __thiscall grid::pickUp(void)" (?pickUp@grid@@QAEXXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: void __thiscall grid::putDown(int,int)" (?putDown@grid@@QAEXHH@Z) already defined in grid.obj
1>Source.obj : error LNK2005: "public: void __thiscall grid::putDown(void)" (?putDown@grid@@QAEXXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: bool __thiscall grid::rightIsClear(void)const " (?rightIsClear@grid@@QBE_NXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: int __thiscall grid::row(void)const " (?row@grid@@QBEHXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "private: void __thiscall grid::setIcon(void)" (?setIcon@grid@@AAEXXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: void __thiscall grid::toggleShowPath(void)" (?toggleShowPath@grid@@QAEXXZ) already defined in grid.obj
1>Source.obj : error LNK2005: "public: void __thiscall grid::turnLeft(void)" (?turnLeft@grid@@QAEXXZ) already defined in grid.obj
1>C:\Users\isaiah\C++\Letter\Debug\Letter.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
这是主文件(source.cpp):

…和grid.cpp(453行)

//文件:grid.cpp
//来自:C++计算基础,第二版
//Rick Mercer、Franklin、Beedle和同事
#包括//用于endl和对象cout、cin
#函数断言的include//
#包括//用于函数exit、rand()和srand()
#包括//
#包含//类字符串
使用名称空间std;
#包括“grid.h”
//--建设者
网格::网格()
{//默认构造函数
}
grid::grid(inttotalrows,inttotalcols)
{//使用一条逃生路线在边缘设置边界
int r,c;
断言(totalRows=0);
断言(totalCols 0);
showPath=1;//当路径为1时显示路径,当路径为0时保持交点可见
lastRow=totalRows-1;
lastCol=总COLS-1;

对于(r=0;r’检验=‘v’,{校验==`p>编译一个CPP文件时,C++编译器将首先运行预处理器并生成编译单元。编译单元将由源文件组成,所有 >包含由引用的文件内容替换的< /COD>指令。< /P> 在您的代码中,
Source.cpp
文件包含
grid.cpp
。结果是
grid.cpp
的全部内容将内联到编译单元中

然后链接器将尝试链接生成的
Source.obj
grid.obj
文件,但它们都包含
grid.cpp
文件中方法的定义。这是编译器告诉您的

解决方案是将行
#include“grid.cpp”
替换为
#include“grid.h”

编辑

要使
grid.cpp
顶部定义的常量可供
source.cpp
使用,而不包括
grid.cpp
,您需要使用
extern
关键字在
grid.h
中声明它们:

extern const char intersectionChar;
extern const char beenThereChar;
extern const char blockChar;
extern const char thingHereChar;
extern const char moverOnThingChar;
extern const int north;
extern const int east;
extern const int south;
extern const int west;
extern
关键字告诉编译器变量是在另一个编译单元中定义的,它们的地址/值将在链接时解析

编辑

步骤1:在网格.h的顶部插入以下行:

extern const char intersectionChar;
extern const char beenThereChar;
extern const char blockChar;
extern const char thingHereChar;
extern const char moverOnThingChar;
extern const int north;
extern const int east;
extern const int south;
extern const int west;
步骤2:在网格.cpp的顶部插入以下行:

const char intersectionChar = '.';
const char beenThereChar = ' ';
const char blockChar = '#';
const char thingHereChar = 'O';
const char moverOnThingChar = '&';
const int north = 0;
const int east = 1;
const int south = 2;
const int west  = 3;

通过这些更改,使用g++4.8.2一切都可以正常编译。

在source.cpp文件中,更改以下内容:

#include "grid.cpp"
为此:

#include "grid.h"

您可能需要更改CPP问题的标签不幸的是,我已经尝试过了,问题出在source.CPP中,它说:grid letter(13,7,3,3,west);当在.cpp文件中明确定义时,它声称west是未定义的。
extern
定义应该放在头文件中。这意味着编译器将强制一个单元中的非extern定义与另一个单元尝试访问的内容相匹配。@musicman_385,
west
source.cpp
中未定义source.cpp
应该包括
grid.h
,而
grid.h
应该有
extern
定义,列在TAS的答案中。hmm tellin me“非法存储类”,“一个类中只能初始化静态常量整型数据成员”,“未声明的标识符”以及大约一百个其他错误。Im gunna发布了整个c上述grid.cpp的ode^^
const char intersectionChar = '.';
const char beenThereChar = ' ';
const char blockChar = '#';
const char thingHereChar = 'O';
const char moverOnThingChar = '&';
const int north = 0;
const int east = 1;
const int south = 2;
const int west  = 3;
#include "grid.cpp"
#include "grid.h"