c++ – LINK 2005 error based on my own class that I did not double include. How to track down and resolve issue?


I am currently getting the following error:

Error   LNK2005 "unsigned char * fontArray" (?fontArray@@3PAEA) already defined in Chip8.obj    CHIP 8  C:\Users\Cyani\source\repos\CHIP-8\main.obj 1   

I understand that it is saying that I am double defining fontArray and that this is usually caused by including a header that has already been included through a different header. The thing is, I can’t seem to find where I did this. fontArray is located in the Font.h file.

file.h:

#include <cstdint>

const unsigned int FONTSET_SIZE = 80;

uint8_t fontArray[FONTSET_SIZE] =
{
    0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
    0x20, 0x60, 0x20, 0x20, 0x70, // 1
    0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
    0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
    0x90, 0x90, 0xF0, 0x10, 0x10, // 4
    0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
    0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
    0xF0, 0x10, 0x20, 0x40, 0x40, // 7
    0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
    0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
    0xF0, 0x90, 0xF0, 0x90, 0x90, // A
    0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
    0xF0, 0x80, 0x80, 0x80, 0xF0, // C
    0xE0, 0x90, 0x90, 0x90, 0xE0, // D
    0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
    0xF0, 0x80, 0xF0, 0x80, 0x80  // F
};

and it is included in the Chip8.h header:

#pragma once

#include <cstdint>
#include <chrono>
#include <random>
#include "Font.h"

class Chip8
{
public:
    uint8_t registers[16]{};
    uint8_t memory[4096]{};
    uint16_t index{};
    uint16_t pc{};
    uint16_t stack[16]{};
    uint16_t sp{};
    uint8_t delayTimer{};
    uint8_t soundTimer{};
    uint8_t keypad[16]{};
    uint32_t video[64 * 32]{};
    uint16_t opcode;


    Chip8() : randGen(std::chrono::system_clock::now().time_since_epoch().count()) {

        //init rng
        randByte = std::uniform_int_distribution<int>(0, 255);
        //init PC
        pc = START_ADDRESS;


        //load fonts into mem
        for (unsigned int i = 0; i < FONTSET_SIZE; ++i) {
            memory[FONT_ADDRESS + i] = fontArray[i];
        }
        // Set up function pointer table
        table[0x0] = &Chip8::Table0;
        table[0x1] = &Chip8::OP_1nnn;
        table[0x2] = &Chip8::OP_2nnn;
        table[0x3] = &Chip8::OP_3xkk;
        table[0x4] = &Chip8::OP_4xkk;
        table[0x5] = &Chip8::OP_5xy0;
        table[0x6] = &Chip8::OP_6xkk;
        table[0x7] = &Chip8::OP_7xkk;
        table[0x8] = &Chip8::Table8;
        table[0x9] = &Chip8::OP_9xy0;
        table[0xA] = &Chip8::OP_Annn;
        table[0xB] = &Chip8::OP_Bnnn;
        table[0xC] = &Chip8::OP_Cxkk;
        table[0xD] = &Chip8::OP_Dxyn;
        table[0xE] = &Chip8::TableE;
        table[0xF] = &Chip8::TableF;
        ...

So my question is, am I looking in the wrong place? Visual studio says its an issue in the Chip8.obj so I assume it’s an issue in the Chip8.h header.

Leave a Reply

Your email address will not be published. Required fields are marked *