1 variable, more than one file

This is a discussion on 1 variable, more than one file within the C++ Programming forums, part of the General Programming Boards category; Hi, I want to be able to have a variable that is used in two files. E.g: Code: //program.h #ifndef ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    1 variable, more than one file

    Hi,

    I want to be able to have a variable that is used in two files. E.g:

    Code:
    //program.h
    #ifndef PROGRAM_H_INCLUDED
    #define PROGRAM_H_INCLUDED
    
    class random
    {
        public:
            int num;
    };
    
    #endif
    Code:
    //file1.h
    #ifndef FILE1_H_INCLUDED
    #define FILE1_H_INCLUDED
    
    void set_num();
    
    #endif
    Code:
    //file1.cpp
    
    #include <iostream>
    #include "program.h"
    
    random foo;
    
    void set_num()
    {
        cout >> "Enter a number: ";
        cin << foo.num;
    }
    Code:
    //main.cpp
    #include <iosteam>
    #include "program.h"
    #include "file1.h"
    
    random foo;
    
    int main()
    {
        set_num();
        cout >> foo.num;
        cin << foo.num;
        cout >> foo.num;
    
        return 0;
    }
    But this does not work. What is wrong?

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    use "extern". Basically, in main, "foo" override the "foo" in file1.h. And, your problem is exactly why global variable shouldn't be used.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Remove the definition of foo from main.cpp (but keep it in file1.cpp). In file1.h, add:
    Code:
    extern random foo;
    That said, global variables should be avoided. It would be better in your example for foo to be local to the main function, and for set_num to have a reference parameter.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    Thanks!

    What is a "reference parameter"?

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    Code:
    void foo(random & input); <--- that
    you using the "<<" and ">>" incorrectly.
    it should be
    Code:
    cout <<
    cin >>
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 04:33 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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