Calling a header file without a function.

So, I have a pretty big code, so I decided to split it. I created a new file called Questions.h, and wrote into it pretty much everything.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef QUESTIONS_H
#define QUESTIONS_H
#include <cstdlib>
#include <iostream>
#include <vector>
#include <stdio.h>

    FILE * pFile;
    vector <string> Questions;
    vector <string> BQuestions;
    vector <string> ITQuestions;
    vector <string> CQuestions;
    vector <string> MQuestions;
    vector <string> EQuestions;
    vector <string> GeoQuestions;
    vector <string> HQuestions;
    vector <string> GQuestions;
    vector <string> GeQuestions;
    vector <string> MuQuestions;
    vector <string> MoQuestions;
    
    char szBQuestion[255];
    char szITQuestion[255];
    char szCQuestion[255];
    char szMQuestion[255];
    char szEQuestion[255];
    char szMoQuestion[255];
    char szMuQuestion[255];
    char szGQuestion[255];
    char szGeQuestion[255];
    char szHQuestion[255];
    char szGeoQuestion[255];
    pFile = fopen ("qvst.qst", "r+");
    if ( pFile != NULL ) {
         
         while(!feof(pFile)) {
             fgets(szBQuestion, 100, pFile);
             if(strstr(szBQuestion, "IT:"))  {

and so on.
I included the file in the Main.cpp file, but the problem is when I try to compile, I get this error for like 20 times:
1
2
9 C:\Dev-Cpp\Advanced Trivia\Questions.h expected constructor, destructor, or type conversion before '<' token 
9 C:\Dev-Cpp\Advanced Trivia\Questions.h expected `,' or `;' before '<' token 


The main file is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdlib>
#include <iostream>
#include <vector>
#include <stdio.h>
#include "Questions.h"

using namespace std;

int main()
{
    
    for(int i = 0; i < ITQuestions.size() - 1; i++) {
            cout << ITQuestions[i] << endl;
            }
    system("PAUSE");

}

And I also get a
 
12 C:\Dev-Cpp\Advanced Trivia\Trivia.cpp `ITQuestions' undeclared (first use this function)  


How can I call the .h file or how can I make it work this way?
I tried with classes, but classes require a void function (from what I learned) to execute the code.
Is there any way I can make the program execute the code without a function?

Thank you for your time,
BreaKer.
Last edited on
I would imagine your problem is that vector resides in the std namespace, but you did not qualify your vectors with std::. Understandable since your variables once were in a CPP file where you had "using namespace std;".

So use std::vector instead of vector alone. Don't add "using namespace std;" to a header file. It is not good practice because a header file tends to be part of a bigger project. The writer of the project should be able to be in control of which namespaces are implicit and which aren't.

Also note that you will incur in trouble if you include this new header file in more than one CPP file.

As a rule: You define global variables in a single CPP file, customarily called globals.cpp, and then you create a header file customarily called globals.h. In the CPP file goes what you added to your header file, and in the header file you EXTERN your globals.

Example:

globals.cpp
1
2
3
#include "globals.h"

std::vector<string> ITQuestions;


globals.h
1
2
3
4
5
6
7
8
9
10
#pragma once

#ifndef __GLOBALS_H__
#define __GLOBALS_H__

#include <vector>

extern std::vector<string> ITQuestions;

#endif // __GLOBALS_H__ 


Now you can include globals.h in multiple CPP files that need access to the global variables without problems.
And how about the File-reading functions? I need to get them into the Main.cpp so they could work? I did what you said, and now I get errors like:
13 C:\Dev-Cpp\Advanced Trivia\globals.h template argument 1 is invalid 13 C:\Dev-Cpp\Advanced Trivia\globals.h ISO C++ forbids declaration of `MQuestions' with no type
 
13 C:\Dev-Cpp\Advanced Trivia\globals.h `string' was not declared in this scope  

And this is globals.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once

#ifndef __GLOBALS_H__
#define __GLOBALS_H__

#include <vector>


    extern std::vector <string> Questions;
    extern std::vector <string> BQuestions;
    extern std::vector <string> ITQuestions;
    extern std::vector <string> CQuestions;
    extern std::vector <string> MQuestions;
    extern std::vector <string> EQuestions;
    extern std::vector <string> GeoQuestions;
    extern std::vector <string> HQuestions;
    extern std::vector <string> GQuestions;
    extern std::vector <string> GeQuestions;
    extern std::vector <string> MuQuestions;
    extern std::vector <string> MoQuestions;
    
    extern std::char szBQuestion[255];
    extern std::char szITQuestion[255];
    extern std::char szCQuestion[255];
    extern std::char szMQuestion[255];
    extern std::char szEQuestion[255];
    extern std::char szMoQuestion[255];
    extern std::char szMuQuestion[255];
    extern std::char szGQuestion[255];
    extern std::char szGeQuestion[255];
    extern std::char szHQuestion[255];
    extern std::char szGeoQuestion[255];

#endif  
Last edited on
Mine was just an example and I did not mean for it to be perfect. The string datatype is really std::string. And no, the char data type is NOT part of the std namespace. And you also need to #include <string> in your globals.h file because std::string is defined there.
I did include it and compiled it after I posted here, no changes made.
I know those simple examples where you've got in the .h file something like
int sum(int a, int b) {
return a+b;
}

and in your main you just use sum(x, y) and you get the result...Well, that's easy by using a function, but I can't really understand how to call a header without using a functions, as I said, not just for the variables, but for everything (fgets, push_back, whiles, ifs, etc)
When you tell your program to include a header, you are telling it to write everything in the header file at this point in your file. So all of those variables that you declared in your header are technically in the global scope. Any functions you define in a header would be callable from your main file as well. The phrase "Calling a header" is a misnomer.
But the thing is I've got no function to call...I haven't got a void or anything like this to do it...Only the variables. My question would be how can I actually call those and make them work.
Looked around the net, asked everybody and nobody gave me a straight answer. I admit I may be a pain in the arse, but I'm pretty new to this. I've written programs before, but not with headers and stuff...Only little cout - cin "games".
Somebody told me that I have to create classes...Never done that before in my life so I tried...Nothing came up xD
I don't get what your current grief is. If you followed my instructions, you removed the std:: from all those std::char 's, and added std:: to all those string 's; you also added #include <string> to globals.h. You also created globals.cpp with the actual variable definitions (which look exactly the same as the extern's in your header file, but without the extern keyword). All this means that your code must work. But in case it doesn't, just post the compiler errors here and we'll continue helping out.
Well, this is my globals.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "globals.h"

    std::vector <string> Questions;
    std::vector <string> BQuestions;
    std::vector <string> ITQuestions;
    std::vector <string> CQuestions;
    std::vector <string> MQuestions;
    std::vector <string> EQuestions;
    std::vector <string> GeoQuestions;
    std::vector <string> HQuestions;
    std::vector <string> GQuestions;
    std::vector <string> GeQuestions;
    std::vector <string> MuQuestions;
    std::vector <string> MoQuestions;
    
    char szBQuestion[255];
    char szITQuestion[255];
    char szCQuestion[255];
    char szMQuestion[255];
    char szEQuestion[255];
    char szMoQuestion[255];
    char szMuQuestion[255];
    char szGQuestion[255];
    char szGeQuestion[255];
    char szHQuestion[255];
    char szGeoQuestion[255];

And this is the globals.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once

#ifndef __GLOBALS_H__
#define __GLOBALS_H__

#include <vector>
#include <string>

    extern std::vector <string> Questions;
    extern std::vector <string> BQuestions;
    extern std::vector <string> ITQuestions;
    extern std::vector <string> CQuestions;
    extern std::vector <string> MQuestions;
    extern std::vector <string> EQuestions;
    extern std::vector <string> GeoQuestions;
    extern std::vector <string> HQuestions;
    extern std::vector <string> GQuestions;
    extern std::vector <string> GeQuestions;
    extern std::vector <string> MuQuestions;
    extern std::vector <string> MoQuestions;
    
    extern char szBQuestion[255];
    extern char szITQuestion[255];
    extern char szCQuestion[255];
    extern char szMQuestion[255];
    extern char szEQuestion[255];
    extern char szMoQuestion[255];
    extern char szMuQuestion[255];
    extern char szGQuestion[255];
    extern char szGeQuestion[255];
    extern char szHQuestion[255];
    extern char szGeoQuestion[255];


#endif // __GLOBALS_H__  

As for errors, I get this error:
1
2
3
4
9 C:\Dev-Cpp\Advanced Trivia\globals.h `string' was not declared in this scope 
9 C:\Dev-Cpp\Advanced Trivia\globals.h template argument 1 is invalid 
9 C:\Dev-Cpp\Advanced Trivia\globals.h template argument 2 is invalid 
9 C:\Dev-Cpp\Advanced Trivia\globals.h ISO C++ forbids declaration of `Questions' with no type 

For every string I declared in globals.h...
One more time: It is std::string, not string.
Oh, I see, that was stupid of me :D
Now I fixed that problem but I still got one. Where should I put all the functions (couts, fgets, push_Backs) in globals.h or globals.cpp ?
I tried putting it in globals.cpp, and I get these errors:
1
2
29 C:\Dev-Cpp\Advanced Trivia\globals.cpp expected constructor, destructor, or type conversion before '=' token 
29 C:\Dev-Cpp\Advanced Trivia\globals.cpp expected `,' or `;' before '=' token 

for the line:
 
 pFile = fopen ("qvst.qst", "r+");

and
1
2
30 C:\Dev-Cpp\Advanced Trivia\globals.cpp expected unqualified-id before "if" 
30 C:\Dev-Cpp\Advanced Trivia\globals.cpp expected `,' or `;' before "if" 

for the line
 
  if ( pFile != NULL ) {


And if I put it globals.h, I get the same errors, including a new one:
3:1 C:\Dev-Cpp\Advanced Trivia\globals.h unterminated #ifndef
even if Globals.h contains
1
2
3
4
#ifndef __GLOBALS_H__
#define __GLOBALS_H__
// code
#endif 
Last edited on
Statements like the one with fopen() and if()'s go inside a function. They just can't be standalone. I suppose you have a function called main()?
I have the Main function in my main (Trivia.cpp). Somebody told me that there can be only one main() so I shouldn't create another one in the globals.h and globals.cpp file. I tried creating a
void execute() {//fopen/if/etc code } and put it in the .h file, then add execute(); in the Main(), but it didn't work...
And it is true that you cannot have more than one main(). I am not asking you to create a new main function in globals.cpp or any other file for that matter. I am just saying that statements go inside functions. All programs have at least one function: main() (or WinMain() for Windows apps). I am asking that you move the statements inside a function. Which function exactly? I don't know. It is up to you. I just suggested main().

EDIT: It is best if you put your code, BTW. It is faster that way.
Last edited on
Well, the point of the whole thread was to get all those things in a separate file, so I don't have my Main() so full. All of them, in the beginning where in the Main().
How can I create a function that takes no parameters, so I can call it in Main() and make it access my fopen/push_backs/ifs/etc ?
Ok, that's understandable. Just know that only variables can stand alone outside of functions. Statements must be inside functions, unless they are constructing a variable (but let's not deviate from the subject).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//This goes traditionally into globals.cpp because it is considered a global function (see the function's name).
//It is understood that this function is needed in several places of your program, therefore the need to make it a global function.
void MyGlobalFunction()
{
    //Put your statements here.
}

//Now extern the global function in your global header using this next line:
extern void MyGlobalFunction();


//Now in main(), can call it like this:
int main()
{
    ....
    MyGlobalFunction();
    ....
}
Last edited on
It is finally working! The thing I did wrong was that I didn't put the extern void MyFunction() in the .h file.

Thank you very much for your patience and your reply!
Topic archived. No new replies allowed.