Thread: Build error

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    55

    Build error

    Multiple definitions of "board". Here are my files.
    main.cpp
    Code:
    #include <stdlib.h>
    #include "global_functions.h"
    
    int main(int argc, char** argv) {
        initialize_game();
        return (EXIT_SUCCESS);
    }
    global_functions.cpp
    Code:
    #include "global_functions.h"
    #include <iostream>
    #include <stdio.h>
    void initialize_game(){
        //set the board up in the initial configuration
        
        std::cout << "Who goes first?" << std::endl;
        std::cout << "P for player" << std::endl;
        std::cout << "C for computer" << std::endl;
    
        char response;
        std::cin.get(response);}
    Code:
    #ifndef _GLOBAL_FUNCTIONS_H
    #define	_GLOBAL_FUNCTIONS_H
    enum{A, B, C, D, E, COLUMNS};
    enum{PROOK, PPAWN, PKNIGHT, PBISHOP, CBISHOP, CKNIGHT, CPAWN, CROOK, ROWS};
    
    int board[ROWS][COLUMNS]= {{0,1,1,1,0},
                              {1,1,1,1,1},
                              {0,0,1,0,0},
                              {0,0,0,0,0},
                              {0,0,0,0,0},
                              {0,0,2,0,0},
                              {2,2,2,2,2},
                              {0,2,2,2,0}};
    void initialize_game();
    #endif	/* _GLOBAL_FUNCTIONS_H */
    I don't see how the board is being defined multiple times. The file is getting included twice but the inclusion guards should take care of that. Can anybody see what I did wrong?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
    int board[ROWS][COLUMNS]= {{0,1,1,1,0},
                              {1,1,1,1,1},
                              {0,0,1,0,0},
                              {0,0,0,0,0},
                              {0,0,0,0,0},
                              {0,0,2,0,0},
                              {2,2,2,2,2},
                              {0,2,2,2,0}};
    You are initializing this in an include file which is included into multiple files. You should move this to one of the cpp files and then in this header have:

    [code]
    extern int board[ROWS][COLUMNS];

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Quote Originally Posted by jimblumberg View Post
    Code:
    int board[ROWS][COLUMNS]= {{0,1,1,1,0},
                              {1,1,1,1,1},
                              {0,0,1,0,0},
                              {0,0,0,0,0},
                              {0,0,0,0,0},
                              {0,0,2,0,0},
                              {2,2,2,2,2},
                              {0,2,2,2,0}};
    You are initializing this in an include file which is included into multiple files. You should move this to one of the cpp files and then in this header have:

    [code]
    extern int board[ROWS][COLUMNS];

    Jim
    Hmm I see, thanks. I thought the #ifndef directive would have kept it from being defined more than once for the whole program. So things defined with #define are only visible to the file they are in and files that include them, but the "board" variable I declared was global to the whole program? So when I included the file twice it actually processed my header file twice...Is that how it is working?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What it is doing is trying to create 2 variables both named "board" in the global scope. The header file is processed by every file that "includes" it. See this link Basics-of-C-plus-plus-Include-Files for a better explanation.

    JIm

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM