Slight issue with a global declaration

I have a slight problem with a variable set up.

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();
sorry i thought i had corrected the way it would display but you will still will get the idea
Today will be the last full day my partner and i have to finish this up, it is the last issue we have before our game is complete.

any help will be appeciated
globals are evil. The best solution is to not use them.

If you must, a global can exist only in one cpp file. If you want other cpp files to access it, you must have them declared as extern.

1
2
3
// in globals.cpp *only*

int myglobal = 0;

1
2
3
// in globals.h

extern int myglobal;


You can then #include globals.h in whatever file.

But again... ew.
I have it declared in the global header it is declared as extern

however it is still causing an issue, All i have it doing is taking the amount that amtylw

which is a variable i set for player imput on how many chips they want to raise

the lastylw is supposed to store that data and then use it for the call function in the bet cpp

it is declared as extern int lastylw;

and of course it is declared in the raise as lastylw = amtylw;

now ideally i want it so that the 'call' the other player can do has to be at least the last bet or in the case 'lastylw'
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.
i did what you said about initializing it in one cpp which i have done
1
2
3
4
5
int lastylw = 0;
int lastblu = 0;
int lastwht = 0;
int lastgrn = 0;
int lastred = 0;


but when i try to use it it errors

ideally the

lastylw = amtylw is used to put the last amount bet into a variable

but when i try to use it with my bet. More so when i set up my call or re raise of another players bet

when i use my lastylw variable i get the current issue

Error 1 error LNK2001: unresolved external symbol "int lastylw" (?lastylw@@3HA) P1 Bet.obj



Error 1 error LNK2001: unresolved external symbol "int lastylw" (?lastylw@@3HA) P1 Bet.obj


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.
they areall connected cause without this function the game works you just don't have to match the persons bet to call

In the globals.h

1
2
3
4
5
6
7
8
9
10
11
extern int lastylw;
extern int lastblu;
extern int lastwht;
extern int lastgrn;
extern int lastred;

extern int amtylw;
extern int amtblu;
extern int amtwht;
extern int amtgrn;
extern int amtred;


in the raise cpp

the amt group is initialized in the raise cpp

1
2
3
4
5
int amtylw = 0;
int amtblu = 0;
int amtwht = 0;
int amtgrn = 0;
int amtred = 0;


the last group is also initialised in the raise cpp
1
2
3
4
5
int lastylw= 0;
int lastblu = 0;
int lastwht = 0;
int lastgrn = 0;
int lasstred = 0;


after that as i posted before when the raise occurs it is based on the amt for that color
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
if(player2.GetChipsRed() > 0)
					{
						SetConsoleTextAttribute(hOut,
							FOREGROUND_RED |
							FOREGROUND_INTENSITY);
						cout<<"You currently have "<<player2.GetChipsRed()<<" Red Chips\n";
						cout<<"How many chips do you want to bet?\n\n";
						cin>>amtred;
						pot.GainChipsRed(amtred);
						player2.LoseChipsRed(amtred);
					 lastred = amtred;
						wait();
						if((amtred > 0 )&&(amtred < 2))
						{
							SetConsoleTextAttribute(hOut,
								FOREGROUND_RED |
								FOREGROUND_INTENSITY);
							cout<<player2.GetName()<<" puts in "<<amtred<<" Red Chip\n\n";
						}
						if(amtylw > 1)
						{
							SetConsoleTextAttribute(hOut,
								FOREGROUND_RED |
								FOREGROUND_INTENSITY);
							cout<<player2.GetName()<<" puts in "<<amtred<<" Red Chips\n\n";
						}
					}
					else
					{
						cout<<"You have no chips to bet!!";
					}
					SetConsoleTextAttribute(hOut,
						FOREGROUND_RED | 
						FOREGROUND_GREEN | 
						FOREGROUND_BLUE);


int the betting cpp i tired to write it so

call would be

1
2
3
4
5
6
7
cout<<player1.GetName()<<" calls!\n\n";
							player1.LoseChipsRed(lastred);
							pot.GainChipsRed(lastred);
							wait();
							bet1 = true;
							deal();
							break;


however the lastred and the others like it cause me trouble
sorry again i copied the code right out of the program and it didn't format its spacing correctly
lastred is typed incorrectly (lasstred)

Other than that, as long as the variables are declared globally in your cpp file and your cpp file is being linked, this will work fine.
Topic archived. No new replies allowed.