   C++ and Headers/includes.
  I'm having trouble understanding this, I have a header global.h which has all my global variables and constants, I want these to be available to some of my classes and functions (stored in their own cpp) but if I include the header in the .cpp containing said functions or constants my compiler says I've declared the variables/constants from the header file have been defined as many times as I've included them, even with #ifndef. [/run on]I'm sure I'm just doing something wrong here, any tips would be great.   
  Read Organizing Code Files in C and C++;Quote:Original post by OluseyiRead Organizing Code Files in C and C++Thank you but even reading that didn't help me. I'm already using #ifndef HEADER_H_, #DEFINE HEADER_H_ ..code.. #endifEDIT: I'm using Dev-C++ if that matters at all.   ;  Hmm, I'm not sure if you read that entire article thoroughly.  Here's a tested example(code::blocks with mingw)globals.h#ifndef GLOBALS_H_#define GLOBALS_H_extern int alpha;extern int beta;#endifglobals.cpp#include "globals.h"int alpha = 0;int beta = 1;main.cpp#include <iostream>#include "globals.h"int main(){std::cout << alpha << " " << beta << std::endl;return 0;};  Yea, that is precisely what I have.. let me post my code.global.h#ifndef GLOBAL_H_#define GLOBAL_H_#include <allegro.h>#include <alpng.h>#include <stdlib.h>//#define MODE GFX_AUTODETECT_WINDOWED#define MODE GFX_AUTODETECT_FULLSCREEN#define SCREENW 640#define SCREENH 480#define BALLSPEED 3#define PADDLESPEED 2//Colors#define RED makecol(235, 0 ,0)#define BLUE makecol(0, 0, 225)#define WHITE makecol(255, 255 ,255)//Paddlesize#define PADDLEW 29#define PADDLEH 5//Ballsize#define BALLW 3#define BALLH 3/*  Gameload = 0  Gamepause = 1  Gameon = 10  Gamequit = 100*/ extern int gamestate;extern int random;BITMAP *buffer; extern struct paddleTag {    int x, y;    int dir;    int color, score;} paddles[2];extern struct ballTag {    int x, y;    int xspeed, yspeed;    int color;    int status; // 0 = dead, 1 = alive} balls;extern struct moveTag {    int x, y;} move;#endifDraw.h#ifndef DRAW_H_#define DRAW_H_#include "global.h"class eraseClass;class drawClass { private:    void draw(int, int, int);    int x, y;    int objtype; public:    void ball();    void paddle(int);    void screens();    friend class eraseClass;} draw;#endifDraw.cpp//Draw some fun stuff on the screen#include "draw.h"/* Main draw function.This function will determine if it should draw a ball, or a paddle and then do so. */void drawClass::draw(int num, int objtype, int color) {if(objtype == 1){    x = paddles[num].x;    y = paddles[num].y;    rectfill(buffer, x - PADDLEW, y - PADDLEH, x + PADDLEW, y + PADDLEH, color);}else if (objtype == 2){    if (balls.status == 1)    {   x = balls.x;   y = balls.y;   rectfill(buffer, x - BALLW, y + BALLH, x + BALLW, y - BALLH, color);    }}}/* Passes the required variables to draw.draw() to draw a paddle. */void drawClass::paddle(int num) {draw(num, 1, paddles[num].color);}/* Passes the required variables to draw.draw() to draw a ball. */void drawClass::ball() {    draw(1, 2, balls.color);}//Blits all the new drawings to the buffer.void drawClass::screens() {blit(buffer, screen, 0, 0, 0, 0, 640, 480);}Main.cpp/* Ba-BOUNCE! */////////////////#include "global.h"#include "ai.h"#include "draw.h"#include "erase.h"#include "init.h"#include "move.h"#include "titlescreen.h"#include "getinput.h"#include "shutdown.h"using namespace std;int main(void) {    init.startup();   titlescreen();   init.game();   while (gamestate == 10) {   erase.paddle(0);   erase.paddle(1); erase.ball(); motion.paddle(0);   motion.paddle(1); motion.ball();draw.paddle(0);   draw.paddle(1); draw.ball(); getinput();   ai(); if(balls.status == 0) {  init.nextround();   } draw.screens(); rest(10);  }    shutdown();    return 1;    }END_OF_MAIN();;Quote:Original post by Lempfaceglobal.hBITMAP *buffer;Also needs to be extern'd.  Post the actual compile errors that you get.-me;  init.o(.bss+0x0):init.cpp: multiple definition of `init'main.o(.bss+0xd):main.cpp: first defined hereinit.o(.bss+0x4):init.cpp: multiple definition of `draw'main.o(.bss+0x0):main.cpp: first defined hereinit.o(.bss+0x10):init.cpp: multiple definition of `erase'main.o(.bss+0xc):main.cpp: first defined heredraw.o(.bss+0x0):draw.cpp: multiple definition of `draw'main.o(.bss+0x0):main.cpp: first defined hereerase.o(.bss+0x0):erase.cpp: multiple definition of `erase'main.o(.bss+0xc):main.cpp: first defined hereerase.o(.bss+0x4):erase.cpp: multiple definition of `draw'main.o(.bss+0x0):main.cpp: first defined heremove.o(.bss+0x0):move.cpp: multiple definition of `motion'main.o(.bss+0x10):main.cpp: first defined heremain.o(.text+0x25):main.cpp: undefined reference to `gamestate'main.o(.text+0xd8):main.cpp: undefined reference to `balls'init.o(.text+0x8e):init.cpp: undefined reference to `buffer'init.o(.text+0x94):init.cpp: undefined reference to `gamestate'init.o(.text+0x123):init.cpp: undefined reference to `paddles'init.o(.text+0x129):init.cpp: undefined reference to `paddles'init.o(.text+0x133):init.cpp: undefined reference to `paddles'init.o(.text+0x13d):init.cpp: undefined reference to `paddles'init.o(.text+0x147):init.cpp: undefined reference to `paddles'init.o(.text+0x16c):init.cpp: more undefined references to `paddles' followinit.o(.text+0x1b5):init.cpp: undefined reference to `balls'init.o(.text+0x1bb):init.cpp: undefined reference to `balls'init.o(.text+0x1c5):init.cpp: undefined reference to `balls'init.o(.text+0x1cf):init.cpp: undefined reference to `balls'init.o(.text+0x1d9):init.cpp: undefined reference to `balls'init.o(.text+0x1e3):init.cpp: more undefined references to `balls' followinit.o(.text+0x200):init.cpp: undefined reference to `random'init.o(.text+0x205):init.cpp: undefined reference to `random'init.o(.text+0x210):init.cpp: undefined reference to `random'init.o(.text+0x216):init.cpp: undefined reference to `random'init.o(.text+0x21f):init.cpp: undefined reference to `move'init.o(.text+0x22b):init.cpp: undefined reference to `move'init.o(.text+0x251):init.cpp: undefined reference to `random'init.o(.text+0x256):init.cpp: undefined reference to `random'init.o(.text+0x261):init.cpp: undefined reference to `random'init.o(.text+0x267):init.cpp: undefined reference to `random'init.o(.text+0x270):init.cpp: undefined reference to `move'init.o(.text+0x27a):init.cpp: undefined reference to `move'init.o(.text+0x286):init.cpp: undefined reference to `move'init.o(.text+0x290):init.cpp: undefined reference to `move'init.o(.text+0x2ca):init.cpp: undefined reference to `balls'init.o(.text+0x2d4):init.cpp: undefined reference to `balls'init.o(.text+0x2de):init.cpp: undefined reference to `balls'init.o(.text+0x2e8):init.cpp: undefined reference to `paddles'init.o(.text+0x2f2):init.cpp: undefined reference to `paddles'init.o(.text+0x2fc):init.cpp: undefined reference to `paddles'init.o(.text+0x306):init.cpp: undefined reference to `paddles'init.o(.text+0x352):init.cpp: undefined reference to `buffer'init.o(.text+0x37d):init.cpp: undefined reference to `paddles'init.o(.text+0x386):init.cpp: undefined reference to `paddles'init.o(.text+0x3b6):init.cpp: undefined reference to `buffer'init.o(.text+0x412):init.cpp: undefined reference to `buffer'draw.o(.text+0x1e):draw.cpp: undefined reference to `paddles'draw.o(.text+0x36):draw.cpp: undefined reference to `paddles'draw.o(.text+0x77):draw.cpp: undefined reference to `buffer'draw.o(.text+0x8d):draw.cpp: undefined reference to `balls'draw.o(.text+0x98):draw.cpp: undefined reference to `balls'draw.o(.text+0xa2):draw.cpp: undefined reference to `balls'draw.o(.text+0xe3):draw.cpp: undefined reference to `buffer'draw.o(.text+0x143):draw.cpp: undefined reference to `paddles'draw.o(.text+0x16f):draw.cpp: undefined reference to `balls'draw.o(.text+0x1d6):draw.cpp: undefined reference to `buffer'getinput.o(.text+0x12):getinput.cpp: undefined reference to `gamestate'getinput.o(.text+0x2b):getinput.cpp: undefined reference to `paddles'getinput.o(.text+0x44):getinput.cpp: undefined reference to `paddles'getinput.o(.text+0x50):getinput.cpp: undefined reference to `paddles'ai.o(.text+0x4):ai.cpp: undefined reference to `balls'ai.o(.text+0xa):ai.cpp: undefined reference to `paddles'ai.o(.text+0x12):ai.cpp: undefined reference to `paddles'ai.o(.text+0x1d):ai.cpp: undefined reference to `balls'ai.o(.text+0x23):ai.cpp: undefined reference to `paddles'ai.o(.text+0x2b):ai.cpp: undefined reference to `paddles'ai.o(.text+0x37):ai.cpp: undefined reference to `paddles'move.o(.text+0x56):move.cpp: undefined reference to `move'move.o(.text+0x5c):move.cpp: undefined reference to `move'move.o(.text+0x68):move.cpp: undefined reference to `move'move.o(.text+0x95):move.cpp: undefined reference to `move'move.o(.text+0xb6):move.cpp: undefined reference to `paddles'move.o(.text+0xdd):move.cpp: undefined reference to `paddles'move.o(.text+0xe6):move.cpp: undefined reference to `paddles'move.o(.text+0xff):move.cpp: undefined reference to `paddles'move.o(.text+0x126):move.cpp: undefined reference to `paddles'move.o(.text+0x12f):move.cpp: more undefined references to `paddles' followmove.o(.text+0x1ba):move.cpp: undefined reference to `balls'move.o(.text+0x1c6):move.cpp: undefined reference to `balls'move.o(.text+0x1d1):move.cpp: undefined reference to `balls'move.o(.text+0x1db):move.cpp: undefined reference to `balls'move.o(.text+0x1e7):move.cpp: undefined reference to `paddles'move.o(.text+0x1ed):move.cpp: undefined reference to `balls'move.o(.text+0x1fa):move.cpp: undefined reference to `paddles'move.o(.text+0x204):move.cpp: undefined reference to `paddles'move.o(.text+0x20d):move.cpp: undefined reference to `balls'move.o(.text+0x214):move.cpp: undefined reference to `paddles'move.o(.text+0x21d):move.cpp: undefined reference to `balls'move.o(.text+0x224):move.cpp: undefined reference to `paddles'move.o(.text+0x22d):move.cpp: undefined reference to `balls'move.o(.text+0x24e):move.cpp: undefined reference to `move'move.o(.text+0x25c):move.cpp: undefined reference to `paddles'move.o(.text+0x265):move.cpp: undefined reference to `balls'move.o(.text+0x26c):move.cpp: undefined reference to `paddles'move.o(.text+0x275):move.cpp: undefined reference to `balls'move.o(.text+0x27c):move.cpp: undefined reference to `paddles'move.o(.text+0x285):move.cpp: undefined reference to `balls'move.o(.text+0x2a6):move.cpp: undefined reference to `move'move.o(.text+0x2b3):move.cpp: undefined reference to `balls'move.o(.text+0x2bf):move.cpp: undefined reference to `balls'move.o(.text+0x2d8):move.cpp: undefined reference to `move'move.o(.text+0x2e4):move.cpp: undefined reference to `move'move.o(.text+0x2ea):move.cpp: undefined reference to `balls'move.o(.text+0x2ef):move.cpp: undefined reference to `move'move.o(.text+0x2f5):move.cpp: undefined reference to `balls'titlescreen.o(.text+0x4e):titlescreen.cpp: undefined reference to `buffer'titlescreen.o(.text+0x9d):titlescreen.cpp: undefined reference to `buffer'titlescreen.o(.text+0xb0):titlescreen.cpp: undefined reference to `gamestate'titlescreen.o(.text+0x10b):titlescreen.cpp: undefined reference to `buffer'shutdown.o(.text+0x7):shutdown.cpp: undefined reference to `buffer'   ;Quote:Original post by LempfaceYea, that is precisely what I have.. let me post my code.global.h*** Source Snippet Removed ***Draw.h*** Source Snippet Removed ***Draw.cpp*** Source Snippet Removed ***Main.cpp*** Source Snippet Removed ***from the looks of it, you have all your globals externed in global.h, but they arent defined anywhere to begin with. you need a global.cpp or some such implementation file in which you actually declair your globals.;  They are defined in the functions that call them. For instance extern struct paddleTag{.........} paddles[2];is defined in init.cppvoid initClass::game() {//setup paddle0paddles[0].color = RED;paddles[0].x = SCREENW / 2;paddles[0].y = SCREENH - 30;paddles[0].score = 0;paddles[0].dir = 0;//setup paddle1paddles[1].color = BLUE;paddles[1].x = SCREENW / 2;paddles[1].y = 30;paddles[1].score = 0;paddles[1].dir = 0;//setup ballballs.color = WHITE;balls.status = 1;balls.x = SCREENW / 2;balls.y = SCREENH / 2;balls.xspeed = 0;balls.yspeed = 0;/* This starts the ball moving at the start of the game. Positive BALLSPEED will send the ball downwhere -BALLSPEED will send the ball up */random = rand() % 2;if (random == 1){    move.y = BALLSPEED;}else{    move.y = -BALLSPEED;}}Full Source[Edited by - Lempface on June 5, 2006 6:51:29 PM];Quote:Original post by LempfaceThey are defined in the functions that call them. For instance..snipthat's not defining it, that's using it.  You actually need to define it:globals.hextern struct paddleTag{.........} paddles[2];globals.cppstruct paddleTag paddles[2];-me   