How do I do this?

Pages: 12
Ok, so I made a header file that contains a class that I wrote. In order for it to work, it must have const's defined. I want to be able to reuse this header file but I may want to change the value of the const's from file to file. Is there a way I could do something like this? I have looked up on it, something with extern, it no longer says the variable isn't declared, but I can't figure out how to make it have constness I tried extern const int and defining it as const in my .cpp but it doesn't work =(

For example

1
2
//myheader.h
extern int SCR_X; //I want to define it's value in .cpp 


1
2
//main.cpp
const int SCR_X = 640;


Any help is appreciated. Thanks.
Last edited on
consts must be declared and initialized at the same time. I'm not sure if the above is valid, but if it is, you're just overriding int SCR_X with const int SCR_X. If you need different values for different modules you might want to consider using parameters or maybe defines.
Ok thanks. It appears like #define works, though I haven't compiled yet as I have to change some stuff. But I have to do the #defines before I include the header file. I don't see any problems with it though. Thanks again.
You don't have to use the extern keyword in this case.
Move the const int SCR_X = 640; above the include command for the header file.
Wouldn't that generate a duplicate definition error?
Not if you don't put again in the header file the const int SCR_X or in general if you don't declare the SCR_X again...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//main.cpp file
#include <iostream>
using namespace std;
const int SCR_X = 640;//declare it before the header call
#include "header.h"
using namespace std;
int main(){
   showSCR();
   return 0;
}


//header.h
void showSCR(){
   cout << SCR_X << endl; //Just use it :D
}
Last edited on
If that's the case, it doesn't matter much whether he puts the const before or after the inclusion.
If you put it after then you have error:
error C2065: 'SCR_X' : undeclared identifier


I just think the header as part of my code. Replace whether you see #include "header.h" with the code for the "header.h". In that case you use a variable without declaring it... Compiler error...
Thanks guys. I'm a little confused though. I (thought) I read something about constants having external linkage (or file scope of something?), but I can't seem to find it or anything else that backs that up. Maybe I was thinking of this?

Declarations of variables and functions at file scope are external by default.

..but I don't know what file scope is...

Anyway, so does SCR_X have external linkage (or file scope?) in the above case? I'm just asking because I don't want it to just work, I'd like to have a bit of a better understanding about it.

EDIT: I just read something....is file scope the same as global...? So even if it's not constant, I can still use it my header IF its declared before I include the header? Sorry for all the questions, I should probably just try it =P
Last edited on
I read that, too. I don't use globals much, but I don't see why they couldn't have file scope. Note that if the global is declared in a header (as it should be for multifile programs) the global has scope for all files including that header.

Mitsakos: Um... But there shouldn't be any code in the header, and therefore no references to SCR_X and no compiler error. Also, declaring a global outside of the file it's going to be used in is pretty retarded (unless, of course, it's being declared in a header the file in question includes).
Last edited on
File scope means that the variable is only known in the file you declare it.
When you use #include the precompiler put the two files together and they act as a single file.
If you want you can splitt your program in smaller .cpp files. In this case you have to use the extern keyword. That way you make the seperate files know that this variable can change its value in another file.
Consider the following example:
1
2
3
4
5
6
7
8
9
//File main.cpp
#include <iostream>
using namespace std;
int x,y;
void showX_Y();
int main(){
   x=4, y=10;
   showX_Y();
}


1
2
3
4
5
6
7
8
//file main_cont.cpp
#include <iostream>
using namespace std;
extern int x,y;//Global scope
int j;//file scope
void showX_Y(){
   cout << x << ", " << y << endl;
}


The files are in the same project. To use the variables x and y int the main_cont.cpp you have to declare them as extern.
In the main_cont.cpp the variable named "j" has file scope because main.cpp doens't know anything about it.
Hope that helps


EDIT: @helios
But there shouldn't be any code in the header, and therefore no references to SCR_X and no compiler error

Then why we need the header file and the SCR_X variable to be used in the header?
Last edited on
Ah okay, thank you very much. I don't know why but 'file scope' seemed more to me like 'all files in your project' scope. That does make a lot more sense though...brain fart. Thanks again.
Also, I don't know how you tried the extern const int but the following works for me:
1
2
3
4
5
6
7
8
9
//main.cpp
#include <iostream>
using namespace std;
#include "header.h"
const int SCR_X = 640;
int main(){
   showSCR();
   return 0;
}


1
2
3
4
extern const int SCR_X;
void showSCR(){
   cout << SCR_X << endl;
}


This is another use of extern. To use a variable before declaring it in your program.
Because it is used by the file/s that include the header. Without the header, how are we going to use functions defined in a different file without copy-pasting code (and thus creating a linker error) or manually redefining them in each file using them?

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//file0.h
const int var0=458;

int f();

//file0.cpp
int f(){
    return var0;
}

//main.cpp
#include <iostream>
//EDIT:
#include "file0.cpp"
#include "file0.h"

int main(){
    std::cout <<f()+var0<<std::endl; //I've never used a using and I never will
    return 0;
}
Last edited on
Try the example about main.cpp and main_cont.cpp in a project. (I didn't use #include to put the files together).
There is nothing connecting them except that they are in the same project. But it works fine. The project may have many files that will built into one executable for release.

extern
All the programs that you have worked with so far have been quite small. However, in reality, computer programs tend to be much larger. As a program file grows, the compilation time eventually becomes long enough to be annoying. When this happens, you should break your program into two or more separate files. Once you divide your program this way, small changes to one file will not require that the entire program be recompiled. The multiple file approach can yield a substantial time savings with large projects. The extern keyword helps support this approach. Let’s see how.

In programs that consist of two or more files, each file must know the names and types of the global variables used by the program. However, you cannot simply declare copies of the global variables in each file. The reason for this is that in C++, your program can include only one copy of each global variable. Therefore, if you try to declare the global variables needed by your program in each file, you will have trouble. When the linker tries to link together the files, it will find the duplicated global variables, and will not link your program. The solution to this dilemma is to declare all of the global variables in one file and use extern declarations in the others


From "C++ From Ground Up, by Herbert Schildt"

Hope that helps.

[EDIT]:
Because it is used by the file/s that include the header

As mikeb sayed:
I made a header file that contains a class that I wrote. In order for it to work, it must have const's defined. I want to be able to reuse this header file but I may want to change the value of the const's from file to file

He use the class from the header in many programs but he wants to change the value of SCR_X in the program, not in the header.
So the SCR_X is declared in the main program but it is being used in the header...
If I missunderstood correct me please...
Last edited on
Just to add to my stupidity. Like I said I want to reuse this header file. But where do I put it. I have most of my projects in separate folders and it won't work for me if I put it in the /include folder either. I'd like it to be in one place in case I ever make changes to it.

@Mitsakos

Also, I don't know how you tried the extern const int but the following works for me:


I'm might be playing with fire (seems kinda crazy to me) but I have a function that takes a 2d array of pointers, and the last [] must contain a constant. I was no longer get errors that it was undefined, just that it had a non-const value? or something. I dunno, I tried a bunch of ways and I couldn't get it to work.
Last edited on
Not really.
It may be that I haven't built a big enough program yet and there's some compilation time advantage for your method, but it seems to me like a waste of effort to go around copying definitions when #include can do that for you.
If you use include you compile everything each time you press compile. With seperate files you just compile what you changed since the last building. That's why it is time efficient in very big projects.
If I missunderstood correct me please...


You are correct. If it's any help/interest, these two const's are the screen(window) width and height. There's probably a better way to do what I'm doing, but this works, for now at least.
Oops. My bad. Damn typo.
The #include "file0.cpp" should be #include "file0.h"
There. That makes a hell of a lot more sense.

mikeb570: Yes. The dimensions should be local variables, initialized in main(), and passed between the functions using them. You're basically raping structured programming (well, once in a while it doesn't hurt, to put the b***h in its place).
Last edited on
Pages: 12