0

I have recently started learning c++ after learning a good amount of Javascript.

I'm pretty sure that the problem I am facing has to do with scope/how many times the header file is #included, but I need some clarification.

If you look at what I have - you can see that I have "int input" commented out in input.hpp, and I have it actually declared in input.cpp within the scope of the getInput() function. Can someone explain in detail why I am able to compile the way it is written here, but it won't compile if I switch where "int input" is declared? NOTE: The error I get is " multiple definition of `input' "

Also, I know this is a long post so if I need to ask this in another post please tell me. Since I am new to C++, have I separated my files correctly?

These are my files:

main.cpp

#include <iostream>
#include "add.hpp"
#include "input.hpp"

int x = getInput();
int y = getInput();

int main() 
{
    std::cout << add(x, y) << std::endl;
    
    return 0;
}

add.cpp

#include "add.hpp"
#include <iostream>

int addCalc (int x, int y)
{
  return (x + y);
}

std::string add (int x, int y)
{
  return (std::to_string (x) + " + " + std::to_string (y) + " = " + std::to_string (addCalc (x, y)));
}

add.hpp

#ifndef add_hpp
#define add_hpp
#include <iostream>

int addCalc(int x, int y);

std::string add(int x, int y);

#endif

input.cpp

#include "input.hpp"
#include <iostream>

int getInput()
{
    int input;
    
    std::cout << "Enter a number: " << std::endl;
    std::cin >> input;
    
    return input;
}

input.hpp

#ifndef input_hpp
#define input_hpp
#include <iostream>

int getInput();
//int input;

#endif

Any and all comments would be appreciated.

Thanks in advance!

  • 3
    My advice in `c++` is to avoid global variables and try to limit the scope of a variable as much as possible. The `int input;` variable should be a variable of local scope inside the `getInput()` function. Not something exposed in a header. Also if you define a variable in a header you run into the one definition rule. [https://en.cppreference.com/w/cpp/language/definition](https://en.cppreference.com/w/cpp/language/definition) – drescherjm Dec 10 '20 at 18:25
  • 3
    It is not clear to me exactly what information you are missing. To clarify, can you please elaborate on why you think you need `int input;` in the header? Why is it surprising that it compiles with it commented out? – François Andrieux Dec 10 '20 at 18:27
  • 1
    The **input.hpp** file has `#include `, yet there is nothing in that header file that **input.hpp** needs. Never include superfluous header files. Always include every header file that the code in a file (`*.hpp` or `*.cpp`) directly depends upon. Never depend upon indirectly included header files. And avoid globals when possible (e.g., move the int x and int y into inside main's body). – Eljay Dec 10 '20 at 18:31
  • drescherjm, Thanks for your response! When going through the training it seemed to say that all variables should be declared in the .hpp file, and then defined in the .cpp file. Is that not the case? I guess maybe i'm just confused on when something goes in a header. FrançoisAndrieux, I wouldn't be a surprise to me if all I was doing was commenting it out. The issue is why I get an error when I declare the variable in the .hpp instead of in the getInput() scope. Eljay, Thanks for this information. I totally overlooked that! I'll be on the lookout for that in the future. – MurrayJenkins Dec 10 '20 at 18:47
  • @MurrayJenkins The `int input;` in `getInput` declares a local variable, it does not need to be declared anywhere else. Even if you keep both declarations, the one from the header is a global object and the one from the function is a local object. You'll simply have two different `input` variables in the context of `getInput`, the local one shadows (hides) the global one. – François Andrieux Dec 10 '20 at 18:53
  • @MurrayJenkins Often (such as in the case of `int input;`) a declaration is also a definition. You must not have definitions in headers, only declarations.This is because headers are generally compiled multiple times in an application but you must never has multiple definitions of the same object (multiple declarations are okay). Since this seems to be the main focus of the question, I've linked a duplicate question asking the same thing. – François Andrieux Dec 10 '20 at 18:54
  • 1
    Declaring a variable in a header is a sign that the variable is being shared across multiple files. While this is the right thing to do sometimes, it's uncommon and can lead to interesting problems. If it's involved in a bug you are tracking you have to test pretty much all of your code because any function anywhere could be manipulating it. Sometimes you encounter the [Static Initialization Order Fiasco](https://en.cppreference.com/w/cpp/language/siof) where one file makes use of the variable before the file that contains the variable has made it ready for use. – user4581301 Dec 10 '20 at 19:01

0 Answers0