Here’s a simplified version of my code that compiles:
#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;
}
But if I change all instances of pos to position, I get the error: main.obj : error LNK2005: “public: void __thiscall position::print(void)” (?print@position@@QAEXXZ) already defined in position.obj
1>D:\Programming\Test\Debug\Test.exe : fatal error LNK1169: one or more multiply defined symbols found
Oddly, the class used to be named position and compiled fine until I made some changes to how it handles the board array. Can someone explain why I’m getting this error?
Update
Now it doesn’t matter what the class is named, I get the same error. I created a new MSVC++ project, copied the source files, and did a new build. Same error. Here’s the actual code, instead of the concise version.
main.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;
}
position.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";
}
}
(I’m writing a program to figure out simple chess endgames.)
As you can see, position::print() is only defined in one place, contrary to the error message…unless I’m seriously overlooking something. It’s pretty early.
The problem is that you’re compiling the code contained in
position.cpptwice, and you’re trying to linking the compiled code into one file.The problem is caused by this line in
main.cpp:You’re including the C++ source file, not the header. This means that all code in
position.cppis included inmain.cppand then the resulting file is compiled. You then compileposition.cppon it’s own.Try to include
position.hinstead – if there’s no such header yet, define it. The alternative fix is to not addposition.cppto the list of sources to be compiled.