Question

I have a header file with a namespace, and within that namespace I have several events declared and a single function. Within the corresponding .cpp file, I flesh out the function that would give those objects their properties(the objects are unions). However, when I compile the linker claims that the objects have already been declared in the main.obj, when they have not. I feel like this is a problem with me using a namespace, but I can't find anything pointing to that assumption.

The .h

#pragma once
#include <SDL.h>

namespace UserEvents {


    /*Events for the StateMachine*/
     SDL_Event switchToEntryState;
     SDL_Event switchToIntroState;
     SDL_Event switchToGameState;
     SDL_Event switchToPauseState;
     SDL_Event exitState;
     SDL_Event shutdownStateMachine;


    bool initUserStates();
};

And the .cpp

#include "UserEvents.h"


using namespace UserEvents;
/*

SDL_Event switchToEntryState;
SDL_Event switchToIntroState;
SDL_Event switchToGameState;
SDL_Event switchToPauseState;
SDL_Event exitState;
SDL_Event shutdownStateMachine;

*/

/*
***********************************
USER EVENT CODE CHART
***********************************

EVENT CODE | MODULE
--------------------
0          | STATEMACHINE

*/


bool initUserStates() {
    //Attempt to register the number of events
    Uint32 userEventType = SDL_RegisterEvents(6); //Registering 6 custom events
    if (userEventType!= ((Uint32)-1)) {

        //Set up each event
        SDL_zero(switchToEntryState);
        SDL_zero(switchToIntroState);
        SDL_zero(switchToGameState);
        SDL_zero(switchToPauseState);
        SDL_zero(exitState);
        SDL_zero(shutdownStateMachine);

        switchToEntryState.type = userEventType;
        switchToIntroState.type = userEventType;
        switchToGameState.type = userEventType;
        switchToPauseState.type = userEventType;
        exitState.type = userEventType;
        shutdownStateMachine.type = userEventType;

        switchToEntryState.user.code = 0;
        switchToIntroState.user.code = 0;
        switchToGameState.user.code = 0;
        switchToPauseState.user.code = 0;
        exitState.user.code = 0;
        shutdownStateMachine.user.code = 0;

        switchToEntryState.user.data1 = (void*)0;
        switchToIntroState.user.data1 = (void*)1;
        switchToGameState.user.data1 = (void*)2;
        switchToPauseState.user.data1 = (void*)3;
        exitState.user.data1 = (void*)4;
        shutdownStateMachine.user.data1 = (void*)-1;



        return 1;
    }

    return 0;
}
Was it helpful?

Solution

Each file that #includes the header file will have its own definition of the objects. That's why there are multiple definitions.

Instead, you should declare the objects as extern in the header, and then have a single .cpp file that contains those definitions. The extern says "This is just a declaration. The object will be defined elsewhere."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top