Solved static variable problem

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Liduen

Hacker
Dank Tier VIP
May 19, 2013
702
8,478
33
Hi guys!

I wanted to test something and wrote this little piece of code:
C++:
#include <iostream>
#include <windows.h>
#include <math.h>
#include <string>
using namespace std;

class Player{
public:
	static int nStaticTest;
};


int main(){
	Player::nStaticTest = 1;
	cout << Player::nStaticTest << endl;
	system("Pause");
}
But now i get this error:
Unbenannt.JPG

Do I really have to "make" an object out of this class first before I can use this static variable?
What am I doing wrong?


Liduen

PS: I already tried this https://guidedhacking.com/showthread.php?3052-Tutorial-Visual-Studio-Linker-Errors&p=11144&viewfull=1#post11144 and it didn't help.
 
Last edited:

rN'

Jr.Hacker
Meme Tier VIP
Jan 19, 2014
340
5,268
41
Create a instanz/object for the Class.
C++:
class Player
{
public:
	int	iTest;
};

int main()
{
	// Method 1
	Player*	Test1 = new Player;
	Test1->iTest = 1337;

	//Method 2
	Player	Test2;
	Test2.iTest = 1334;

	printf( "%d\n%d\n", Test1->iTest, Test2.iTest );
	system( "pause" );
	return 0;
}
 
Last edited:

Liduen

Hacker
Dank Tier VIP
May 19, 2013
702
8,478
33
Do I really have to create an object to use this static variable?
 

Cyrion

Coder
Dank Tier Donator
Nobleman
Dec 31, 2013
107
618
7
You can use a function like that but not a variable , because it doesnt exist if you dont create an instance first .
 

TastyHorror

Coder
Dank Tier Donator
Nobleman
Oct 11, 2012
179
2,268
8
Do I really have to create an object to use this static variable?
Taken from wiki...
C++:
#include <stdio.h>   

void func() {     
   static int x = 0; // x is initialized only once across three calls of func() and                                  
   // the variable will get incremented three                                   
   //times after these calls. The final value of x will be 3.     
   printf("%d\n", x); // outputs the value of x     
   x = x + 1; 
}   

int main() 
{
   //int argc, char *argv[] inside the main is optional in the particular program     
   func(); // prints 0     
   func(); // prints 1     
   func(); // prints 2     return 0;
 }
So the answer is no.
 

Liduen

Hacker
Dank Tier VIP
May 19, 2013
702
8,478
33
Okay I tried it like this:

C++:
class Player{
public:
	static int nStaticTest;
};

int main(){
	Player Test;
	Test.nStaticTest = 1;
	cout << Test.nStaticTest << endl;
	system("Pause");
	return 0;
}
And I get the same error:
View attachment 2480
 

Syperus

RTFM
Meme Tier VIP
Dank Tier Donator
Oct 29, 2012
432
2,638
7
To use it how you're wanting you need to initialize it
C++:
int Player::nStaticTest;
or if you wanted to initialize with a baseline (which is recommended) you could do

C++:
int Player::nStaticTest = 1;
So your code will look like
C++:
class Player{
public:
    static int nStaticTest;
};
 
int Player::nStaticTest = 1;
 
int main(){
   Player::nStaticTest = 1;
    cout << Player::nStaticTest << endl;
	system("Pause");
}
Whenever using static members within a class you need to declare it globally as if you were prototyping (forward declaration) it. This is because the static variable is a member of the class and not an object of the class so it's treated similarly to a function.
 
Last edited:
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods