Thread: Global Classes

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    180

    Global Classes

    How do I make a class item global? This is what i'm doing now....(i'm using borland c++ builder 6.0)


    extern int x;
    extern int y ;
    extern Tank1 *Tank = new Tank1;
    extern TImage *Sprite_Tank1 = new TImage(Owner);

    in a .h file

    Tank1 *Tank = new Tank1;
    TImage *Sprite_Tank1 = new TImage(Owner);
    int x;
    int y ;
    int i;

    y=200;
    x=100 ;

    in the main .cpp file under a button click

    yes i've included the .h file in the main .cpp file!

    As long as I don't do the extern thingy for {TImage *Sprite_Tank1 = new TImage(Owner);} , it compiles, but isnt global, but when I do the extern thingy, it says "[C++ Error] Whack.h(15): E2451 Undefined symbol 'Owner'"

    Any suggestions?


    Thanks

    DW
    Code:
    extern int x;

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    40
    Put it in code tags before I attempt to read it.
    Code:
    #include <iostream.h>
    int var;
    int test();
    int main() { 
     cout << "Please input your language:\n 1. C (C,C++,C#)\n 2. VB\n 3. Other\n";
     cin >> var;
     return test(); }
    int test() {  
     if(var == 1) {
      cout << "Y0u 4r3 t3h 1337\n";
      system("PAUSE");
      return main(); }
     else if(var == 2) {
      cout << "N00B3R!\n";
      system("PAUSE");
      return main(); }
     else if(var == 3) {
      cout << "You were not thought of.\n";
      system("PAUSE");
      return main(); }
     else {      
      return 0; }}

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Global Classes

    Originally posted by Death_Wraith
    How do I make a class item global? This is what i'm doing now....(i'm using borland c++ builder 6.0)
    Do you mean a single item from a class? You don't.
    Do you mean a an instance of the class? Define it outside of all functions, including main().

    Originally posted by Death_Wraith
    extern int x;
    extern int y ;
    extern Tank1 *Tank = new Tank1;
    extern TImage *Sprite_Tank1 = new TImage(Owner);

    in a .h file
    You cannot use extern and create a variable at the same time. The extern simply tells the compiler that "this variable is not defined here but watch for it -- it will be used here"

    Originally posted by Death_Wraith
    Any suggestions?
    Define the variable in a c-source file, specify only the
    Code:
    extern Tank1 *Tank;
    in the header file.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    180

    Re: Re: Global Classes

    Do you mean a an instance of the class? Define it outside of all functions, including main().
    I tired this, but the compiler complains.

    "[C++ Error] Whack.h(15): E2451
    Undefined symbol 'Owner'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes and Global Functions
    By d_heyzie in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2007, 07:57 PM
  2. Array of Global Classes
    By CrymsonSoul in forum C++ Programming
    Replies: 8
    Last Post: 06-05-2006, 12:48 AM
  3. Declaring classes in global space
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 10-12-2005, 02:46 PM
  4. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM
  5. global classes?????
    By werdy666 in forum C++ Programming
    Replies: 1
    Last Post: 05-18-2002, 11:20 AM