I am making a poker game for a school project. I have created a variable to store the last bet made for each chip set i have created.
I declared it globally since i am using multiple cpp files however i am now stuck with this error:
Error 1 error LNK2001: unresolved external symbol "int lastred" (?lastred@@3HA) P1 Bet.obj
any help would be appreciated.
the basis is int lastylw = amtylw;
due to the setup of the raise system
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
if(player1.GetChipsYlw() > 0)
{
SetConsoleTextAttribute(hOut,
FOREGROUND_RED |
FOREGROUND_GREEN |
FOREGROUND_INTENSITY)
cout<<"You currently have "<<player1.GetChipsYlw()<<" Yellow Chips\n";
cout<<"How many chips do you want to bet?\n\n";
cin>>amtylw;
pot.GainChipsYlw(amtylw);
player1.LoseChipsYlw(amtylw);
int lastylw = amtylw;
wait();
You have to put the non extern variable in 1 and only 1 cpp file.
1 2 3 4
// in globals.cpp *only*
// Don't skip this! Important!
int myglobal = 0;
You know how you can have a function prototype and a function body, right? You must include the prototype in every file that uses it, but the function body can only be in 1 and only 1 cpp file.
It's the same with global variables. The extern declaration is like the "prototype", and the non-extern declaration is the "body". By only having the extern, you have a prototype, but no actual variable. That's why you get the linker error.
This error means you're not doing what you claim to be doing.
If you put this: int lastylw;globally in one cpp file, this error will go away.
The only way this error will persist is if you are spelling it wrong, or if you don't link to the cpp file (make sure the cpp file is part of your project).
If you're still having trouble I'll need to see the full cpp file as well as the header containing your globals.