Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7549779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:02:13+00:00 2026-05-30T10:02:13+00:00

Here’s a simplified version of my code that compiles: #include <iostream> class pos {

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:02:15+00:00Added an answer on May 30, 2026 at 10:02 am

    The problem is that you’re compiling the code contained in position.cpp twice, and you’re trying to linking the compiled code into one file.

    The problem is caused by this line in main.cpp:

    #include "position.cpp"
    

    You’re including the C++ source file, not the header. This means that all code in position.cpp is included in main.cpp and then the resulting file is compiled. You then compile position.cpp on it’s own.

    Try to include position.h instead – if there’s no such header yet, define it. The alternative fix is to not add position.cpp to the list of sources to be compiled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code, which takes two version identifiers in the form 1, 5,
Here is my code (Say we have a single button on the page that
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here's my situation. I've noticed that code gets harder to maintain when you keep
Here's my code: // Not all headers are relevant to the code snippet. #include
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.