致命错误LNK1169:在C++Visual Studio中编写战舰游戏时发现的一个或多个多重定义的符号
fatal error LNK1169: one or more multiply defined symbols found when coding battleship game in C++, Visual Studio
我正在用C++编写一个战舰游戏,但我无法让打印板代码工作。我收到以下错误致命错误LNK1169:找到一个或多个乘法定义的符号。任何帮助将不胜感激。
这是代码:
主代码
// Project 1.cpp: Defines the entry point for the console application.
//
#include "stdafx.h"
#include "printboard.cpp"
int main()
{
printboard();
return 0;
}
印板代码
#include "stdafx.h"
#include < iostream>
#include < iomanip>
using namespace std;
void printboard()
{
int NUM_COLS = 10, NUM_ROWS = 10; //added by me
cout << right;
cout << "n =====> Current Board Status <=====n";
int row, col;
// Print out the column heading values for the board
cout << setw(7) << "0";
for (col = 1; col < NUM_COLS; col++)
cout << setw(3) << col;
cout << endl; // terminate the column heading line
cout << setw(7) << "-";
for (col = 1; col < NUM_COLS; col++)
cout << setw(3) << "---";
cout << endl;
int board[10][10]; //added by me
// Print out each row of the board
for (row = 0; row < NUM_ROWS; row++)
{
cout << setw(3) << row << ":";
for (col = 0; col < NUM_COLS; col++)
{
// if the position to print out is a boat, print out
// a . instead. Otherwise, print out the contents
// of the position
if (board[row][col] >= '1' && board[row][col] <= '4')
cout << setw(3) << '.'; // hide the boat numbers
else
cout << setw(3) << board[row][col];
}
cout << endl;
}
}
在此行中(在其中一个代码文件中(,您将包含另一个代码文件。
#include "printboard.cpp"
这有效地获取了作为包含代码文件的一部分编译的包含的代码文件。
当您编译包含的代码文件本身时,其中定义的所有内容都会再次编译。
通过制作一个单独的标题"printboard.h"来修复,只包含声明:
void printboard();
然后将该标头包含在包含代码文件和包含的代码文件中。不要包含代码文件。
相关文章:
- vscode g++链路故障:体系结构x86_64的未定义符号
- 在 Mac 上使用 CMAKE 将 FFTW 和 FFTWPP 链接到项目中时未定义的符号
- 在C++中使用内联方法时出现未定义的符号错误
- 引擎节点:未定义的符号:_ZTV6Config
- 为函数定义符号不明确的指针参数
- 未定义的引用,尽管存在符号,但 std::experimental::可选
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- Windows 链接器是否使用 LoadLibrary 解析 DLL 中未定义的符号?
- C++ 中 dlsym 的未定义符号
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 仅在 MacOS 上析构函数的未定义符号
- 重复符号 --- 定义和声明之间的差异
- 符号定义多次
- C++部分模板专用化和多符号定义错误
- 使用Visual Studio 2015时有多个符号定义
- CMake和Dylib:符号定义
- g++说引用是未定义的,即使' nm '列出了我的目标文件中的符号定义
- 为什么't my include保护防止递归包含和多个符号定义
- 多个符号定义,但我只定义我的符号一次
- 用c++科学和工程符号定义Flex实类型