Defining/Declaring and Header Files/Source Files

Hi guys, I'm a new programmer that could use some help...

I've been reading everywhere that in general:
*each source file should have a matching header file
*the source file should #include the matching header file
*declare stuff in the header file
*define stuff in source file

Seems simple. I was playing around with it and I came upon a huge mental roadblock regarding declaring/defining variables. Here's my problem in pseudocode:

Scenario 1:
1
2
// header file test.h   
void test();  // declaring test() 

1
2
3
// source file test.cpp
#include "test.h"
void test() cout << "moo";  // defining test() 


This seems to work perfectly fine when I call test() in the main.cpp However, when I try this with variables:

Scenario 2 (doesn't work):
1
2
// header file test.h
int test;  // declaring test 

1
2
3
// source file test.cpp
#include "test.h"
test = 5;  // defining test 


It gives me an "expected constructor" error. Since I #include'd the header file, I thought test was already declared/constructed. After doing some reading, I found that in order to declare global variables, I have to have the extern prefix:

Scenario 3 (does work):
1
2
// header file test.h
extern int test;  // declaring test 

1
2
3
// source file test.cpp
#include "test.h"
int test = 5;  // defining test 


This only kind-of makes sense to me, since it looks like its declaring the variable twice, but ignoring it the first time. Can somebody explain to me why Scenario 2 doesn't work? I know its some kind of scope issue, but if I include the header file which has the declaration, then why can't I simply do test = 5?
Extern means that the variable test is found in another file. You need to do this because including a file is basically the same thing as pasting it where the #include is. Imagine:

1
2
3
4
5
6
7
int test;
test = 5;

int main()
{

}


test = 5 has absolutely no meaning, because it's not part of any code block. The only things you can have outside of a code block are declarations.
int test = 5 works, because the compiler can optimize it to int test(5), which simply means constructing test with the value of 5, instead of explicitly assigning 5.

So you use extern, which lets your compiler know that the integer test has been declared elsewhere, but to let the current file use it. So theoretically you could do

 
extern int baldFISH_123;


And use it in your code, but if it is not declared somewhere you will get a linker error.

And finally, it looks like a declaration because the compiler doesn't know ANYTHING about the external var test except for what you tell it. So you need to let it know that test is an int. It's kind of like declaring functions before defining them. The compiler needs to know what goes in and what comes out, but what goes on inside the function doesn't really matter.
Ahh I see. The test=5 outside the code block example makes perfect sense. I was trying to do similar stuff like pushing back vectors outside a code block in my source file and wondering what the errors were about... D;

I'm still kind of having trouble figuring out where to declare what kind of variables though. For example, I wrote a simple function that would draw a menu with a crude border from a vector containing menu choices

1
2
3
void DrawMenu(vector<string> MenuChoices){
   // function block in here
}


However, I'm confused as to where to declare/define MenuChoices. If I declare in a header file and define in matching the source file, I would have to change the source file every time for different applications (i.e. for different menus), which doesn't seem to make sense since I'll be using it for future programs as well.

In these cases, am I supposed to declare MenuChoices in the header and define in the main() function? Or just do everything in the main() function? I also read that some people just throw all their globals in a separate header file to keep main.cpp relatively clean?
Topic archived. No new replies allowed.