So I'm having an issue with declaring a global variable (in the header) and using it. I've done some reading on header files and how they work, but I can't understand why my code isn't working.
Here's what I have (in a simplified manner):
Main:
// main.cpp
#include "source.hpp"
int main()
{
return variable;
}
Source:
// source.cpp
#include "source.hpp"
variable = 17;
Header:
// source.hpp
#ifndef __SOURCE_HPP_INCLUDED__
#define __SOURCE_HPP_INCLUDED__
extern int variable;
#endif // __SOURCE_HPP_INCLUDED__
I've tried with and without extern in the header file. I get this error when I try to (compile in the source): 'variable' does not name a type What am I not understanding properly?
Also, when I declare the same variable in main.cpp, the compiler does throw back an error about 'redefining' the variable. Why is that?