Thread: code organization fusturation!!!!!

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071

    Angry code organization fusturation!!!!!

    ok....example....I have two .cpp files each with their own header file....they each require a definition (for multitexturing). If i define the variable in both headers, the .cpp files whine and complain that it was defined in the other file...if i but the definition in the .cpp itself, I get a million linker errors say that it was already defined in all the other .cpp files !?

    I can't figure this out...my code is a bit sloppy, and not particularly well written...could that be the problem?

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    // put me in the shared .h file
    extern int foo;
    Code:
    // put me in ONE of the .cpp files
    int foo = 0;

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    now...you say "put me in the shared .h file"....but i said each .cpp has there OWN header....will this make a diference?

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    In one of the .cpp files put:

    int myexternvar;

    In the other .cpp file (and any other ones you create), put

    extern int myexternvar;

    You could also put the extern declarations in the header files instead of the .cpp files if you wanted to.

    When you put the extern keyword in a variable declaration, it means that you are not actually declaring the variable. What is says to the compiler is that this variable is declared somewhere else, but I can use it here.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > "put me in the shared .h file".
    Like common.h

    Code:
    // I'm common.h
    extern int foo;

    Code:
    // I'm foo.cpp
    #include "foo.h"
    #include "common.h"
    int foo = 0;
    Code:
    // I'm bar.cpp
    #include "bar.h"
    #include "common.h"
    Of course, you could put
    extern int foo;
    into foo.h, and do this
    Code:
    // I'm bar.cpp
    #include "foo.h"
    #include "bar.h"
    #include "common.h"

  6. #6
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Hurray! I got it to work! (i guess fresh air helps somewhat )...anyway...thanks all!

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM