Reputation: 2722
I have a class, in it is a private
member variable called which is an int
. For some reason, if I change its value in a method (on the constructor, for example), it will change just fine. But if I change it on a different method and use printf
to output what its contents are on yet another different method, the value is not carried over and is changed into a very very large number.
Header:
class Fruit {
private:
int m_fruitState; // 0 = IDLE, 1 = GROWING, 2 = READY, 3 = FALLEN, 4 = ROTTEN
int m_fruitTimer;
public:
Fruit ( );
int getFruitState( ); // Returns m_fruitState
void setFruitState( int fState );
void growFruit( CCTime dt ); // Called every 1 second (CCTime is a cocos2d-x class)
};
CPP
#include "Fruit.h"
Fruit::Fruit( ) {
// Set other member variables
this -> setFruitState( 0 ); // m_fruitState = 0
this -> m_fruitTimer = 0;
this -> m_fruitSprite -> schedule( schedule_selector( Fruit::growFruit ), 1.0 ); // m_fruitSprite is a CCSprite (a cocos2d-x class). This basically calls growFruit() every 1 second
}
int getFruitState( ) {
return this -> m_fruitState;
}
void setFruitState( int state ) {
this -> m_fruitState = state;
}
void growFruit( CCTime dt ) {
this -> m_fruitTimer++;
printf( "%d seconds have elapsed.", m_fruitTimer );
printf( "STATE = %d", this -> m_fruitState ); // Says my m_fruitState is a very big number
// This if condition never becomes true, because at this point, m_fruitState = a very big number
if ( this -> getfruitState( ) == 0 ) { // I even changed this to m_fruitState == 0, still the same
if ( this -> m_fruitTimer == 5 ) { // check if 5 seconds have elapsed
this -> setFruitState( 1 );
this -> m_fruitTimer = 0;
}
}
}
And then on the main, I make an instance of MyClass.
I have no idea why that happens. Why does C++ do that and how do I fix it? Thanks in advance.
Upvotes: 0
Views: 2658
Reputation: 66441
The "selector" argument to schedule
should be a SEL_SCHEDULE
, where
typedef void(CCObject::* SEL_SCHEDULE)(float)
i.e it should be a member function of a CCObject
.
It is also supposed to be a member of the object you call schedule
on, otherwise the target when it's called will be wrong.
I suspect that this
this -> m_fruitSprite -> schedule( schedule_selector( Fruit::growFruit ), 1.0 );
causes a call to Fruit::growFruit
with this
pointing at the sprite, not the fruit, which leads to all kinds of unpleasantness.
(Note that schedule_selector
does a C-style cast, which means that it's inherently unsafe. Don't use it.)
Upvotes: 3
Reputation: 182819
changeInt( int newInt ); // Assume newInt = 5
Remove the int
from the above line.
void doSomething( ); {
Remove the ;
from the above line.
Update: Now you're missing a ;
from the end of the header file. Fixing all the obvious bugs (that would likely keep it from even compiling), it works fine for me. Either there's still a difference between the code you pasted and the real code, or you've found a compiler bug.
Constructor: myInt = 0
changeInt( int ) : myInt = 5
After constructor and calling changeInt(), myInt = 5
Upvotes: 3