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
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?
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.
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
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.
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
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
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.
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:
externvoid MyGlobalFunction();
//Now in main(), can call it like this:
int main()
{
....
MyGlobalFunction();
....
}