Why am I getting a linker error with my global variables?

I setup two global variables in my header file and I used the extern key word to declare them, but im still getting a linker error saying its an undefined reference?
8 Replies
split
split2y ago
Did you initialize them?
Revan
Revan2y ago
do i have to intialize them in the same header file? or can they be intialized in another file like in w1_p1.cpp in the starter code the prof gave us?
split
split2y ago
You have to initialize them somewhere The extern keyword just means "this variable exists somewhere, dw about it" now your job is to make sure it does exist somewhere and is initialized.
Revan
Revan2y ago
wouldn't this count as intialization in the workshop file?
split
split2y ago
No, initialize it at the top because the compiler needs to know it's value at compile time. Set it to zero at the top You need to initialize it when making the variable Likeint tax = 0 My phone's about to die
Revan
Revan2y ago
so intialize it in the workshop file? not in the header? and ty
split
split2y ago
I don't know where you used extern You have to specify it in a file where you did NOT use extern
Revan
Revan2y ago
yeah i was able to initialize it in my other cpp file thank you