Duplicate error, help needed

I am making a little command line game and I can't seem to fix this weird error. It says duplicate error of my objects, but I am using #pragma once

The full error:
duplicate symbol _GameState in:
/Users/prummi/Library/Developer/Xcode/DerivedData/Mastermind-fottybxidepfogekmopuyksleerv/Build/Intermediates.noindex/Mastermind.build/Debug/Mastermind.build/Objects-normal/x86_64/main.o
/Users/prummi/Library/Developer/Xcode/DerivedData/Mastermind-fottybxidepfogekmopuyksleerv/Build/Intermediates.noindex/Mastermind.build/Debug/Mastermind.build/Objects-normal/x86_64/StateMachine.o
duplicate symbol _Statemachine in:
/Users/prummi/Library/Developer/Xcode/DerivedData/Mastermind-fottybxidepfogekmopuyksleerv/Build/Intermediates.noindex/Mastermind.build/Debug/Mastermind.build/Objects-normal/x86_64/main.o
/Users/prummi/Library/Developer/Xcode/DerivedData/Mastermind-fottybxidepfogekmopuyksleerv/Build/Intermediates.noindex/Mastermind.build/Debug/Mastermind.build/Objects-normal/x86_64/StateMachine.o
duplicate symbol _myCode in:
/Users/prummi/Library/Developer/Xcode/DerivedData/Mastermind-fottybxidepfogekmopuyksleerv/Build/Intermediates.noindex/Mastermind.build/Debug/Mastermind.build/Objects-normal/x86_64/main.o
/Users/prummi/Library/Developer/Xcode/DerivedData/Mastermind-fottybxidepfogekmopuyksleerv/Build/Intermediates.noindex/Mastermind.build/Debug/Mastermind.build/Objects-normal/x86_64/StateMachine.o
ld: 3 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  #include "Code.hpp"
#include "StateMachine.hpp"
#include "ObjectDeclerations.hpp"

int main() {
    
    
    GameState = GameStates::INTRO; // make the GameState intro so the program starts with intro
    
    switch ( GameState ) // handle the different game states
    {
        case GameStates::INTRO:
            Statemachine.processIntro();
            
        case GameStates::MAIN_MENU:
            Statemachine.processMainMenu();
            
        case GameStates::PLAYING:
            Statemachine.processPlaying();
            
        case GameStates::CODECRACKED:
            Statemachine.proccesCodeCracked();
            
        case GameStates::EXIT:
            Statemachine.processExit();
            break;
        
    }
}




ObjectDeclerations.hpp

#pragma once

Statemachine Statemachine; // Create Statemachine object containing the proccessing functions of the different states
Code myCode; // Create a code object containing the games code the user needs to crack

GameStates GameState; // Create enum object for the different gamestates




Code.cpp

void Code::printCode()
{
    std::cout << "The code is ";
    for ( int i = 0; i < 4; i++ )
    {
        std::cout << code[i] << " ";
    }
    std::cout << std::endl;
}

void Code::printGuess()
{
    std::cout << "The code guess is ";
    for ( int i = 0; i < 4; i++ )
    {
        std::cout << guess[i] << " ";
    }
    std::cout << std::endl;
}

void Code::generateCode()
{
    srand( static_cast<unsigned int>(time(NULL)) ); // initialise random seed
    for(int i = 0; i < 4; i++)
    {
        randomNumber = rand() % 6 + 1; // give the randomNumber variable a random number between 1 and 6
        code[i] = randomNumber;
    }
}

void Code::insertCode()
{
    for ( int i = 0; i < 4; )
    {
        std::cout << "Enter code number " << i + 1 << std::endl;
        std::cin >> code[i];
        if( code[i] > 0 && code[i] < 7 )
        {
            i++;
        }
        else
        {
            std::cout << "input invalid, try again" << std::endl;
        }
    }
}

void Code::insertGuess()
{
    for ( int i = 0; i < 4; )
    {
        std::cout << "Enter code number " << i + 1 << std::endl;
        std::cin >> guess[i];
        if( guess[i] > 0 && guess[i] < 7 )
        {
            i++;
        }
        else
        {
            std::cout << "input invalid, try again" << std::endl;
        }
    }
}

int Code::checkGuess()
{
    int goodGuesses = 0;
    
    for(int i = 0; i < 4; i++)
    {
        if ( code[i] == guess[i] )
        {
            goodGuesses++;
        }
    }
    
    return goodGuesses;
}




Code.hpp

#pragma once

class Code
{
public:
// variables
    int randomNumber;
private:
    int code[4]; // array containing the code numbers
    int guess[4]; // array containing the guesses of the code
public:
// functions
    void printCode();
    void generateCode();
    void insertCode();
    
    void insertGuess();
    void printGuess();
    
    int checkGuess();
};




StateMachine.cpp

#include <iostream>
#include "StateMachine.hpp"
#include "Code.hpp"
#include "ObjectDeclerations.hpp"

// intro
void Statemachine::processIntro()
{
    std::cout << "Welcome to mastermind!" << std::endl;
    std::cout << "The point in this game is to guess the code, containing 4 numbers of 1 to 6." << std::endl;
    std::cout << "After every guess you will be told how many numbers were correct" << std::endl;
    std::cout << "" << std::endl;
}

// main menu
void Statemachine::processMainMenu()
{
    std::cout << "MAIN MENU:" << std::endl;
    mainMenu = true; // set mainMenu to true to enter the main menu loop
    
    while(mainMenu) // enter main menu loop
    {
        std::cout << "Enter 1 to generate a random code" << std::endl;
        std::cout << "Enter 2 to put in a code yourself" << std::endl;
        std::cin >> mainMenuChoice;
        
        switch (mainMenuChoice)
        {
            case 1:
                myCode.generateCode();
                std::cout << "Code set" << std::endl;
                mainMenu = false;
                break;
                
            case 2:
                // insert a code
                myCode.insertCode();
                std::cout << "Code set" << std::endl;
                mainMenu = false;
                break;
                
            default:
                std::cout << "Choice invalid, try again" << std::endl;
                break;
        }
    }
}

// playing
void Statemachine::processPlaying()
{
    std::cout << "Good luck cracking the code!" << std::endl << std::endl;
    attempt = 1;
    playing = true; // set playing to true to enter the playing loop
    while (playing)
    {
        std::cout << std::endl;
        std::cout << "Starting attempt number " << attempt << ":" << std::endl;
        myCode.insertGuess();
        myCode.printGuess();
        std::cout << "You had " << myCode.checkGuess() << " correct number(s)" << std::endl; // print how many numbers were guessed correct
        if (myCode.checkGuess() == 4)
        {
            playing = false;
        }
        attempt++;
    }
}

// code cracked
void Statemachine::proccesCodeCracked()
{
    std::cout << "Congratulations, you cracked the code!" << std::endl;
    std::cout << "This took you " << attempt << " attempts" << std::endl;
    GameState = GameStates::INTRO;
}


// exit
void Statemachine::processExit()
{
    std::cout << "" << std::endl;
    std::cout << "EXITING GAME" << std::endl;
}




StateMachine.hpp

#pragma once

enum class GameStates {INTRO, MAIN_MENU, PLAYING, CODECRACKED, EXIT}; // Create enum with gamestates

class Statemachine
{
public:
// Viarable declerations
    int mainMenuChoice;
    int attempt;
    bool mainMenu;
    bool playing;



// Function declerations
    void processIntro();
    void processMainMenu();
    void processPlaying();
    void processExit();
    void proccesCodeCracked();
};




Could anybody tell me how to fix this? Thanks!
#pragma once only helps with preventing the same header file from being included more than once in the same translation unit. If you include ObjectDeclerations.hpp in multiple translation units the Statemachine, myCode and GameState objects will get defined multiple times which is not allowed.

If you want these global variables you need to declare them as extern in the header ...

ObjectDeclerations.hpp
1
2
3
extern Statemachine Statemachine;
extern Code myCode;
extern GameStates GameState;

... and then put the definitions in a source file instead.

ObjectDeclerations.cpp
1
2
3
Statemachine Statemachine;
Code myCode;
GameStates GameState;


If you're using C++17 another alternative is to make them inline.

ObjectDeclerations.hpp
1
2
3
inline Statemachine Statemachine;
inline Code myCode;
inline GameStates GameState;

Then you don't need to add anything extra to the source file.
Last edited on
Topic archived. No new replies allowed.