c++ - "position"是 C++ 中的保留字吗?

标签 c++ visual-c++

这是我编译的代码的简化版本:

#include <iostream>

class pos
{
    public:
        char (&board)[64];
        pos(char (&arr)[64])
            : board(arr)
        {
            *board = *arr;
        }
        void print();
};

void pos::print()
{
    for (int y=0; y<8; y++)
    {
        for (int x=0; x<8; x++)
            std::cout << (int) board[x + y*8] << " ";
        std::cout << "\n";
    }
}

int main()
{
    char test[64] = {
        0, 1, 2, 3, 4, 5, 6, 7,
        1, 2, 3, 4, 5, 6, 7, 8,
        2, 3, 4, 5, 6, 7, 8, 9,
        3, 4, 5, 6, 7, 8, 9,10,
        4, 5, 6, 7, 8, 9,10,11,
        5, 6, 7, 8, 9,10,11,12,
        6, 7, 8, 9,10,11,12,13,
        7, 8, 9,10,11,12,13,14 };

    pos p(test);
    p.print();

    std::cin.get();
    return 0;
}

但是如果我将 pos 的所有实例更改为 position,我会得到错误:main.obj : error LNK2005: "public: void __thiscall position::print( void)"(?print@position@@QAEXXZ) 已在 position.obj 中定义 1>D:\Programming\Test\Debug\Test.exe : fatal error LNK1169: 找到一个或多个多重定义的符号

奇怪的是,该类以前被命名为 position 并且编译得很好,直到我对其处理 board 数组的方式进行了一些更改。有人可以解释为什么我会收到此错误吗?

更新

现在不管类的名称是什么,我都会得到同样的错误。我创建了一个新的 MSVC++ 项目,复制了源文件,并进行了新的构建。同样的错误。这是实际代码,而不是简洁版本。

主要.cpp

#include <iostream>
#include "position.cpp"

int main()
{
    char test[64] = {               // Simple mate test position; TODO: move to test suite
        OO,BK,OO,OO,OO,OO,OO,OO,
        OO,OO,OO,OO,OO,OO,OO,WR,
        OO,WK,OO,OO,OO,OO,OO,OO,
        OO,OO,OO,OO,OO,OO,OO,OO,
        OO,OO,OO,OO,OO,OO,OO,OO,
        OO,OO,OO,OO,OO,OO,OO,OO,
        OO,OO,OO,OO,OO,OO,OO,OO,
        OO,OO,OO,OO,OO,OO,OO,OO };

    position pos(test);
    pos.print();

    std::cin.get();

    return 0;
}

位置.cpp

#include <iostream>

// Board-centric representation; pass as 64-byte array

#define WK 0x0  // White King   
#define WQ 0x1  // White Queen
#define WR 0x2  // White Rook
#define WB 0x3  // White Bishop
#define WN 0x4  // White Knight
#define WP 0x5  // White Pawn
#define BK 0x6  // Black King
#define BQ 0x7  // Black Queen
#define BR 0x8  // Black Rook
#define BB 0x9  // Black Bishop
#define BN 0xA  // Black Knight
#define BP 0xB  // Black Pawn
// 0xC: unused
// 0xD: unused
// 0xE: unused
#define OO 0xF  // Empty

class position
{
    public:
        char (&board)[64];
        position(char (&arr)[64])   // TODO: 'board' dies if the variable passed through 'arr' dies. Allocating memory might fix this. See 'new' and 'shared_ptr'.
            : board(arr)
        {
            *board = *arr;
        }
        void print();
};

void position::print()
{
    for (int y=0; y<8; y++)
    {
        for (int x=0; x<8; x++)
            std::cout << (int) board[x + y*8] << " ";
        std::cout << "\n";
    }
}

(我正在编写一个程序来计算简单的国际象棋残局。)

如您所见,position::print() 仅在一处定义,与错误消息相反……除非我严重忽略了某些内容。现在还早。

最佳答案

问题是您正在编译 position.cpp 两次 中包含的代码,并且您正试图将编译后的代码链接到一个文件中。

问题是由 main.cpp 中的这一行引起的:

#include "position.cpp"

您包含的是 C++ 源文件,而不是 header 。这意味着 position.cpp 中的所有代码都包含在 main.cpp 中,然后编译生成的文件。然后你自己编译 position.cpp

尝试包含 position.h - 如果还没有这样的 header ,请定义它。另一种解决方法是不将 position.cpp 添加到要编译的源列表中。

关于c++ - "position"是 C++ 中的保留字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9429009/

相关文章:

C++ - const 在这里代表什么?

c++ - 使用整数标识符在共享内存上构造对象

c++ - 'for loop' 的终止条件是否在 VC++ 6 中刷新?

c++ - 在std::wstring上使用std::remove_if的警告(MSVC C++ 20)

c++ - 使用 openCV 检测 ROI

c++ - Qt 同步类可以与 MSVC 编译器一起使用吗

c++ - 使用Visual Studios C++ list 文件

c++ - 变量 'PlayerInfo::HistoryGuess'未初始化。始终初始化成员变量(类型6)错误不断出现

c++ - 如何在 C++ 中将文本文件解析为结构的不同元素?

c++ - 最近开发的任何好的灵活的 C++ 样式检查器?