Thread: 1 variable, more than one file

  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
    463
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    463
    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, 03:33 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12: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, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM