• Amused
  • Angry
  • Annoyed
  • Awesome
  • Bemused
  • Cocky
  • Cool
  • Crazy
  • Crying
  • Down
  • Drunk
  • Embarrased
  • Enraged
  • Friendly
  • Geeky
  • Godly
  • Happy
  • Hateful
  • Hungry
  • Innocent
  • Meh
  • Piratey
  • Poorly
  • Sad
  • Secret
  • Shy
  • Sneaky
  • Tired
  • Wtf
  • At Work
  • CodenzHub
  • Coding
  • Deejaying
  • Donating
  • Drinking
  • Eating
  • Editing
  • Hacking
  • Hate Mailing
  • Jamin'
  • Lagging
  • Live Streaming
  • Lurking
  • No Status
  • Pawning
  • PC Gaming
  • PS Gaming
  • Raging
  • Reversing
  • Sleeping
  • Steam Gaming
  • Trolling
  • TwitchStreamer
  • Vodka!
  • Watching TV/Movie
  • Xbox Gaming
  • Youtuber
  • Zombies
  • Results 1 to 9 of 9
    1. #1
      Hacker
      Turning to page 394...
       
      Coding
       
      Liduen's Avatar
      Join Date
      May 2013
      Location
      Germany
      Posts
      713
      Thanks (-->)
      161
      Thanks (<--)
      226

      static variable problem

      Silent VPN
      Hi guys!

      I wanted to test something and wrote this little piece of code:
      #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:
      Click image for larger version. 

Name:	Unbenannt.JPG 
Views:	13 
Size:	27.3 KB 
ID:	2479

      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...ll=1#post11144 and it didn't help.
      Last edited by Liduen; 02-13-2014 at 10:11 AM.
      My contributions
      UrbanTerror | OpenGL - ModelLogger | Polymorphic junk code | Tutorial/Article Collection
      Admiring technological singularity

    2. #2
      Jr.Hacker
      Haze-Productions
       
      Coding
       
      rN''s Avatar
      Join Date
      Jan 2014
      Posts
      340
      Thanks (-->)
      20
      Thanks (<--)
      126
      Create a instanz/object for the Class.
      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 by rN'; 02-13-2014 at 10:53 AM.

    3. #3
      Hacker
      Turning to page 394...
       
      Coding
       
      Liduen's Avatar
      Join Date
      May 2013
      Location
      Germany
      Posts
      713
      Thanks (-->)
      161
      Thanks (<--)
      226
      Do I really have to create an object to use this static variable?
      My contributions
      UrbanTerror | OpenGL - ModelLogger | Polymorphic junk code | Tutorial/Article Collection
      Admiring technological singularity

    4. #4
      Coder
      C++(Qt)/C# Coder
       
      Coding
       
      Cyrion's Avatar
      Join Date
      Dec 2013
      Location
      Canada,Québec
      Posts
      110
      Thanks (-->)
      6
      Thanks (<--)
      16
      You can use a function like that but not a variable , because it doesnt exist if you dont create an instance first .

      Actual project in progress :
      - Cloud File storage using Qt (In the same style as Dropbox)

      static variable problem

    5. Thanks till0sch thanked for this post
    6. #5
      Coder
      Creating UE3 x86/x64 tut
       
      Coding
       
      TastyHorror's Avatar
      Join Date
      Oct 2012
      Location
      Canada
      Posts
      184
      Thanks (-->)
      54
      Thanks (<--)
      82
      Quote Originally Posted by Liduen View Post
      Do I really have to create an object to use this static variable?
      Taken from wiki...
      Code:
      #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.
      Quote Originally Posted by squeenie View Post
      And just like Jesus did on easter Monday, he returns!

    7. #6
      Hacker
      Turning to page 394...
       
      Coding
       
      Liduen's Avatar
      Join Date
      May 2013
      Location
      Germany
      Posts
      713
      Thanks (-->)
      161
      Thanks (<--)
      226
      Okay I tried it like this:

      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:
      Attachment 2480
      My contributions
      UrbanTerror | OpenGL - ModelLogger | Polymorphic junk code | Tutorial/Article Collection
      Admiring technological singularity

    8. #7
      Jr.Hacker
      Haze-Productions
       
      Coding
       
      rN''s Avatar
      Join Date
      Jan 2014
      Posts
      340
      Thanks (-->)
      20
      Thanks (<--)
      126
      Code:
      int nTest
      You dont need a static integer

    9. #8
      Hacker
      Turning to page 394...
       
      Coding
       
      Liduen's Avatar
      Join Date
      May 2013
      Location
      Germany
      Posts
      713
      Thanks (-->)
      161
      Thanks (<--)
      226
      Quote Originally Posted by rN' View Post
      Code:
      int nTest
      You dont need a static integer
      Thanks now it works!
      - but why not with a static variable?
      My contributions
      UrbanTerror | OpenGL - ModelLogger | Polymorphic junk code | Tutorial/Article Collection
      Admiring technological singularity

    10. #9
      RTFM
      Shootin Pool!
       
      Coding
       
      Syperus's Avatar
      Join Date
      Oct 2012
      Location
      127.0.0.1
      Posts
      436
      Thanks (-->)
      101
      Thanks (<--)
      131
      Silent VPN
      To use it how you're wanting you need to initialize it
      int Player::nStaticTest;


      or if you wanted to initialize with a baseline (which is recommended) you could do

      int Player::nStaticTest = 1;


      So your code will look like
      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 by Syperus; 02-14-2014 at 11:33 AM.
      Blacks Ops Zombies, BioShock Infinite Array of Bytes
      "People say I'm a bad influence. I say the world's already fucked, I'm just adding to it" -Eminem

    11. Thanks Liduen thanked for this post

    Similar Game Hacker Threads

    1. [Help] DLL Injection : How to store variable into memory of injected proc
      By babacool123 in forum Hacking Help
      Replies: 1
      Last Post: 03-23-2016, 08:44 PM
    2. [Tutorial] OllyDbg Variable Editing
      By Retsoen in forum Game Hacking Tutorials
      Replies: 1
      Last Post: 02-03-2015, 08:16 PM
    3. [Help] C++ The variable does not change within the function.
      By PlayTheRace in forum C/C++
      Replies: 6
      Last Post: 12-12-2013, 07:32 PM
    4. Static Address
      By danivegas in forum Hacking Help
      Replies: 5
      Last Post: 10-25-2012, 02:54 PM

    Tags for this Thread