0

How do I use variables that are in my main file (main.cpp) in other files (foo.h, foo.cpp)?
No code is really necessary but I'll post some to help clarify my question.

main.cpp

#include<iostream>
#include<foo.h>

using namespace std;

int aa = 10;
int bb = 20;

Foo xyz;

int main() {
    cout<<"Hello World"<<endl;
    xyz.doSomething();
    return 0;
}

foo.h

#ifndef FOO_H
#define FOO_H


class Foo
{
    public:
        void doSomething() {
            int abc = aa + bb;
            cout<<"aa + bb = "<<abc<<endl;
        };
};

#endif // FOO_H
Zak The Hat
  • 317
  • 1
  • 5
  • 15
  • 7
    That’s a **really bad idea**. Don’t do this. Pass them as arguments instead. – Konrad Rudolph Feb 16 '18 at 09:40
  • Can you provide a link or give an example, please? – Zak The Hat Feb 16 '18 at 10:27
  • https://softwareengineering.stackexchange.com/q/148108/2366 — The short answer is that having global variables makes it impossible to create small, self-contained composable solutions. But composing small, self-contained solutions is the *only way* to write complex programs because humans have small brains and can’t think of the whole program at once. – Konrad Rudolph Feb 16 '18 at 10:48

1 Answers1

3

You should declare a main.h file, declare your variable there and declare it later in the main.cpp. So you would have

main.h

extern int aa, bb;

main.cpp

#include "main.h"
#include <iostream>
#include <foo.h>

using namespace std;

int aa = 10;
int bb = 20;

Foo xyz;

int main() {
    cout<<"Hello World"<<endl;
    xyz.doSomething();
    return 0;
}

Then you can just include main.h and use aa and bb

Jerome Reinländer
  • 1,227
  • 1
  • 10
  • 26
  • 4
    *"and declare it later in the main.cpp"* -> *"and define it later in the main.cpp"* – HolyBlackCat Feb 16 '18 at 09:42
  • I think I get what you're saying. But should I also include ``main.h`` in my main.cpp? – Zak The Hat Feb 16 '18 at 09:44
  • 4
    global variables are usually something that you should try to avoid. IMO this may be a working example but a very bad one. – default Feb 16 '18 at 09:44
  • 1
    @Default Yes, I agree very much. This is veeeery bad code, but the answer to this question. Please don't do that, pass variables as parameters and avoid global variables. – Jerome Reinländer Feb 16 '18 at 09:45
  • @JeromeReinländer then why don't you add that to your question? – default Feb 16 '18 at 09:45
  • @Default Strictly speaking, the answer I posted is the answer to the question, and my comment is a comment about the coding style. So I feel like everything is where it belongs. – Jerome Reinländer Feb 16 '18 at 09:47
  • Instead of `extern` can I have `Public`. – Zak The Hat Feb 16 '18 at 09:47
  • @ZakTheHat they're not the same things. `public` is for class members. – DodgyCodeException Feb 16 '18 at 09:48
  • @ZakTheHat `extern` means you will define the variable... well, externally. `public` sets the visibility of a member. These are completely different concepts. – Jerome Reinländer Feb 16 '18 at 09:50
  • @ZakTheHat yes, it's a good idea to include `main.h` in your `main.cpp` because it can catch inadvertent mistakes that wouldn't otherwise get caught; e.g. if you declare `extern int aa;` in main.h but define it as `float aa = 1.234;` in main.cpp. – DodgyCodeException Feb 16 '18 at 09:50
  • @Default Why is it a bad Idea to use this method? – Zak The Hat Feb 16 '18 at 10:00
  • @ZakTheHat global variables are hard to maintain. Depending on how you want to use your variable(s) there are other methods of letting `Foo` know of them. One example being for instance as parameters to `doSomething` – default Feb 16 '18 at 10:07
  • @ZakTheHat when I type "Why are g" into Google, it autocompletes it as "Why are global variables bad?" It is a question that has a lot of websites explaining why it is bad. – DodgyCodeException Feb 16 '18 at 10:08
  • I'm getting errors saying: ``undefined reference to variableName`` – Zak The Hat Feb 16 '18 at 10:10
  • @DodgyCodeException If you type "Why are g" into duckduckgo you would not get those results. Google has tailored it's experience to you. Instead of saying "just google it" you might want to suggest some specific question on stackoverflow or other resource. – default Feb 16 '18 at 10:10
  • @ZakTheHat you need to `#include "main.h"` in foo.cpp. – DodgyCodeException Feb 16 '18 at 10:14
  • @DodgyCodeException I'm getting errors in ``main.cpp`` and I also have ``#include "main.h"`` – Zak The Hat Feb 16 '18 at 10:16
  • I just forgot to declare the variables in ``main.cpp`` as well as ``main.h`` – Zak The Hat Feb 16 '18 at 10:23
  • Do I need to make the variables in ``main.cpp`` pointers to the one declared in ``main.h``? – Zak The Hat Feb 16 '18 at 10:26
  • @ZikTheHat No, that's not necessary. What you are doing in the `main.h` is similar to a forward declaration of a class. You basically say "There will be a variable named aa of type int, but I am not defining that here" – Jerome Reinländer Feb 16 '18 at 11:24