code organization fusturation!!!!!

This is a discussion on code organization fusturation!!!!! within the Windows Programming forums, part of the Platform Specific Boards category; ok....example....I have two .cpp files each with their own header file....they each require a definition (for multitexturing). If i define ...

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

    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
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    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,070
    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
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > "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,070
    Hurray! I got it to work! (i guess fresh air helps somewhat )...anyway...thanks all!

    -psychopath
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

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, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21