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
Did you initialize them?
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?
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.
wouldn't this count as intialization in the workshop file?
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
Like
int tax = 0
My phone's about to dieso intialize it in the workshop file? not in the header?
and ty
I don't know where you used extern
You have to specify it in a file where you did NOT use extern
yeah i was able to initialize it in my other cpp file thank you