#include <iostream>
usingnamespace std;
externint * addCount_ptr; // Not sure if 'extern' is needed..?
void someLoop(int);
int main()
{
cout << "Insert a number of times to loop: ";
int loopCount = 0;
cin >> loopCount;
cout << "\nYou entered: " << loopCount;
someLoop(loopCount);
return 0;
}
void someLoop(int x)
{
addCount_ptr = &x; // undefined reference to 'addCount_ptr'
*addCount_ptr = (x + 1);
cout << "We will now loop once more than you entered."
<< endl;
for (int i = 0; i < x; ++i)
{
cout << "This is loop: " << i
<< endl;
}
}
// I'm wondering if there is some modifier, keyword, or anything that will let the compiler know that this pointer will be given a value at run-time once the user has some input? Although I also think there may be more to it than that. No need to point out that I could have done this to 'loopCount' in main(), my goal is the understanding of this scenario, as I feel I may come across this in some advanced situation later on.
It's the extern keyword that's causing the problem (because you don't need it).
Extern is used to share a global variable between files. It tells the linker that the line is an explicit declaration and nothing more. You essentially promise the linker that you will define it elsewhere, which is what it is complaining about here. Definitions of extern variables are usually put into a file other than the one in which they are declared (usually in source files to prevent being defined more than once).
1 2 3 4 5 6
int* addCount_ptr; // Implicit definition
externint* addCount_ptr; // Explicit declaration
int* addCount_ptr = 0; // Explicit definition (usually in a different file)
addCount_ptr = &x; // Assignment - not a definition
// Hah thank you very much. I used extern because I wanted to make it global and I looked through my book for the closest thing that sounded global. I wasn't sure that it didn't require anything but it makes sense since it's out of any scopes(other than the program scope).
Now I may continue!