致命错误LNK1169:在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();

然后将该标头包含在包含代码文件和包含的代码文件中。不要包含代码文件。

最新更新