Storing a value permanently in a Class

class  Hands {
private:

    int card1_Num;
    int card2_Num;
    int card3_Num;
    int card1_Suit;
    int card2_Suit;
    int card3_Suit;
    bool Card1_selected;
    bool Card2_selected;
    bool Card3_selected;

public:

void setCard1 (int CN, int CS){ //card number, card suit
    card1_Num = CN;
    card1_Suit = CS;
    Card1_selected = true;
    }

    void setCard2 (int CN, int CS){

        card2_Num = CN;
        card2_Suit = CS;
        Card2_selected = true;
        }

        void setCard3 (int CN, int CS){

            card3_Num = CN;
            card3_Suit = CS;
            Card3_selected = true;
            }

void setCard1_selected (bool Status){
    Card1_selected = Status;
}
    void setCard2_selected (bool Status){
        Card2_selected = Status;
    }
        void setCard3_selected (bool Status){
            Card3_selected = Status;
        }


int getCard1 (){

    return card1_Num;
}


    int getCard2 (){

        return card2_Num;
    }

            int getCard3 (){

                return card3_Num;
            }


int getCard1_Suit (){

    return card1_Suit;
}


        int getCard2_Suit (){

            return card2_Suit;
        }


                int getCard3_Suit (){

                    return card3_Suit;
                }

bool getCard1_selected(){
    return Card1_selected;
}

     bool getCard2_selected(){
         return Card2_selected;
    }

            bool getCard3_selected(){
                 return Card3_selected;
            }



    Hands (){

     card1_Num = 0;
     card2_Num = 0;
     card3_Num = 0;
     card1_Suit = 0;
     card2_Suit = 0;
     card3_Suit = 0;
     Card1_selected = false;
     Card2_selected = false;
     Card3_selected = false;

    }//default constructor modify this to make it random when the game starts using constructor

};
This is my class, when I access it in the Setup function and modify a value, the value is changed and stored, but as soon as it is referenced again in another function such as Draw, the value resets to the default constructor.

How can I force the class to hold that data to be visible and modifiable to all functions in the ofApp.cpp file?

Any class should behave as you are saying. CAn you post the code of your ofApp or however you are using this class?
on the other hand, why don’t you use arrays to hold your variables. Your code will become so much shorter, thus less prone to errors.

best

Maybe you made the mistake of declaring a Hands instance in setup() THEN, redeclaring another one in draw().

You Hands instance should be declared in your ofApp.h header file, then used everywhere in ofApp methods.

void ofApp::setup(){

void Instructions (string Names[], string Suit[]);
void Drawcards(int Turn, string Suit[], string Names[], Hands &Player1, Hands &Player2, Hands &Player3, Hands &Player4, vector & Pile);
void Show_hand (string Names[], string Suit[], Hands Player1);

    ofSetCircleResolution(60);

        cout << "GAME 99\n";

        Instructions (Names, Suit);

        Hands Player1, Player2, Player3, Player4;// creating hands



        cout << "All players will now draw 3 cards each \n";


    for (int loop=0 ; loop <3 ; loop++){

        for (Turn = 1 ; Turn <= 4; Turn++){
           Drawcards(Turn, Suit, Names, Player1, Player2, Player3, Player4, Pile);

        }
    }

cout << “You can play one card at a time, and you will gain a card automatically after you draw \n”;

Show_hand (Names, Suit, Player1); //shows hand

value = Player1.getCard1();

        Background.loadImage("images/Background.png");
        Card_B_L.loadImage("images/CardbackL.png");
        Card_B_R.loadImage("images/CardbackR.png");
        Card_B_T.loadImage("images/CardbackT.png");
        Ace_h.loadImage("images/ace of hearts.png");
        Ace_d.loadImage("images/ace of diamonds.png");
        Ace_c.loadImage("images/ace of clubs.png");

    Ace_s.loadImage("images/ace of spades.png");

    two_h.loadImage("images/2 of H.png");

     two_d.loadImage("images/2 of D.png");

    two_c.loadImage("images/2 of C.png");

     two_s.loadImage("images/2 of S.png");


     three_h.loadImage("images/3 of H.png");

     three_d.loadImage("images/3 of D.png");

     three_c.loadImage("images/3 of C.png");

     three_s.loadImage("images/3 of S.png");



      four_h.loadImage("images/4 of H.png");

  four_d.loadImage("images/4 of D.png");

    four_c.loadImage("images/4 of C.png");

     four_s.loadImage("images/4 of S.png");


      five_h.loadImage("images/5 of H.png");

     five_d.loadImage("images/5 of D.png");

     five_c.loadImage("images/5 of C.png");

     five_s.loadImage("images/5 of S.png");


     six_h.loadImage("images/6 of H.png");

    six_d.loadImage("images/6 of D.png");

     six_c.loadImage("images/6 of C.png");

     six_s.loadImage("images/6 of S.png");


      seven_h.loadImage("images/7 of H.png");

     seven_d.loadImage("images/7 of D.png");

     seven_c.loadImage("images/7 of C.png");

     seven_s.loadImage("images/7 of S.png");


      eight_h.loadImage("images/8 of H.png");

     eight_d.loadImage("images/8 of D.png");

     eight_c.loadImage("images/8 of C.png");

     eight_s.loadImage("images/8 of S.png");


      nine_h.loadImage("images/9 of H.png");

     nine_d.loadImage("images/9 of D.png");

     nine_c.loadImage("images/9 of C.png");

     nine_s.loadImage("images/9 of S.png");


      ten_h.loadImage("images/10 of H.png");

     ten_d.loadImage("images/10 of D.png");

     ten_c.loadImage("images/10 of C.png");

     ten_s.loadImage("images/10 of S.png");


      J_h.loadImage("images/J of H.png");

      J_d.loadImage("images/J of D.png");

      J_c.loadImage("images/J of C.png");

      J_s.loadImage("images/J of S.png");


      Q_h.loadImage("images/Q of H.png");

      Q_d.loadImage("images/Q of D.png");

      Q_c.loadImage("images/Q of C.png");

      Q_s.loadImage("images/Q of S.png");


      K_h.loadImage("images/K of H.png");

      K_d.loadImage("images/K of D.png");

      K_c.loadImage("images/K of C.png");

      K_s.loadImage("images/K of S.png");



        intro.loadImage("images/push.png");

}

used in set up here, then used in draw below

void ofApp::draw(){
void Show_hand (string Names[], string Suit[], Hands Player1);

//ofBackground(mouseX/7 ,mouseY/5, b, mouseY);

ofColor colorOne;
ofColor colorTwo;

colorOne.set (mouseX/8 + 70, 0, 0);
colorTwo.set (0, 0, mouseY/8);

//ofBackgroundGradient(colorOne, colorTwo, OF_GRADIENT_CIRCULAR);




ofNoFill();
ofEnableSmoothing();

ofCircle(0,0, radius );
ofCircle(0,720, radius );
ofCircle(1280,0, radius );
ofCircle(1280,720, radius );
intro.draw(0,0);

if (started == true)
{

ofBackgroundGradient(colorOne, colorTwo, OF_GRADIENT_CIRCULAR);

    Background.draw(0,0,0);



    Card_B_L.draw(-35,139);
    Card_B_L.draw(-35,293);
    Card_B_L.draw(-35,445);

	Card_B_R.draw(1120,139);
	Card_B_R.draw(1120,293);
	Card_B_R.draw(1120,445);

	Card_B_T.draw(420,-55);
	Card_B_T.draw(574,-55);
	Card_B_T.draw(726,-55);

p=1;
Suit_P = Player1.getCard1_Suit ();
//card = Player1.getCard1 ();
card = value;
if (p == 1){
x_from_main = p_Card_1_x;

    y_from_main = p_Card_1_y;

}
else if (p == 2){
x_from_main = p_Card_2_x;
y_from_main = p_Card_2_y;

}
else if (p == 3 ){
x_from_main = p_Card_3_x;
y_from_main = p_Card_3_y;
}

switch (Suit_P){
case 0:

if (card == 0){
Ace_h.draw( x_from_main, y_from_main );
}
if (card == 1){
two_h.draw( x_from_main, y_from_main );
}
if (card== 2){
three_h.draw( x_from_main, y_from_main );
}
if (card == 3){
four_h.draw( x_from_main, y_from_main );
}
if (card == 4){
five_h.draw( x_from_main, y_from_main );
}
if (card== 5){
six_h.draw( x_from_main, y_from_main );
}
if (card== 6){
seven_h.draw( x_from_main, y_from_main );
}
if (card == 7){
eight_h.draw( x_from_main, y_from_main );
}
if (card == 8){
nine_h.draw( x_from_main, y_from_main );
}
if (card== 9){
ten_h.draw( x_from_main, y_from_main );
}
if (card == 10){
J_h.draw( x_from_main, y_from_main );
}
if (card == 11){
Q_h.draw( x_from_main, y_from_main );
}
if (card== 12){
K_h.draw( x_from_main, y_from_main );
}

break;

case 1:

if (card == 0){
Ace_d.draw( x_from_main, y_from_main );
}
if (card == 1){
two_d.draw( x_from_main, y_from_main );
}
if (card== 2){
three_d.draw( x_from_main, y_from_main );
}
if (card == 3){
four_d.draw( x_from_main, y_from_main );
}
if (card == 4){
five_d.draw( x_from_main, y_from_main );
}
if (card== 5){
six_d.draw( x_from_main, y_from_main );
}
if (card== 6){
seven_d.draw( x_from_main, y_from_main );
}
if (card == 7){
eight_d.draw( x_from_main, y_from_main );
}
if (card == 8){
nine_d.draw( x_from_main, y_from_main );
}
if (card== 9){
ten_d.draw( x_from_main, y_from_main );
}
if (card == 10){
J_d.draw( x_from_main, y_from_main );
}
if (card == 11){
Q_d.draw( x_from_main, y_from_main );
}
if (card== 12){
K_d.draw( x_from_main, y_from_main );
}

break;

case 2:

if (card == 0){
Ace_s.draw( x_from_main, y_from_main );
}
if (card == 1){
two_s.draw( x_from_main, y_from_main );
}
if (card== 2){
three_s.draw( x_from_main, y_from_main );
}
if (card == 3){
four_s.draw( x_from_main, y_from_main );
}
if (card == 4){
five_s.draw( x_from_main, y_from_main );
}
if (card== 5){
six_s.draw( x_from_main, y_from_main );
}
if (card== 6){
seven_s.draw( x_from_main, y_from_main );
}
if (card == 7){
eight_s.draw( x_from_main, y_from_main );
}
if (card == 8){
nine_s.draw( x_from_main, y_from_main );
}
if (card== 9){
ten_s.draw( x_from_main, y_from_main );
}
if (card == 10){
J_s.draw( x_from_main, y_from_main );
}
if (card == 11){
Q_s.draw( x_from_main, y_from_main );
}
if (card== 12){
K_s.draw( x_from_main, y_from_main );
}

break;

case 3:

if (card == 0){
Ace_c.draw( x_from_main, y_from_main );
}
if (card == 1){
two_c.draw( x_from_main, y_from_main );
}
if (card== 2){
three_c.draw( x_from_main, y_from_main );
}
if (card == 3){
four_c.draw( x_from_main, y_from_main );
}
if (card == 4){
five_c.draw( x_from_main, y_from_main );
}
if (card== 5){
six_c.draw( x_from_main, y_from_main );
}
if (card== 6){
seven_c.draw( x_from_main, y_from_main );
}
if (card == 7){
eight_c.draw( x_from_main, y_from_main );
}
if (card == 8){
nine_c.draw( x_from_main, y_from_main );
}
if (card== 9){
ten_c.draw( x_from_main, y_from_main );
}
if (card == 10){
J_c.draw( x_from_main, y_from_main );
}
if (card == 11){
Q_c.draw( x_from_main, y_from_main );
}
if (card== 12){
K_c.draw( x_from_main, y_from_main );
}

break;

default:
break;

}

    ofCircle(X, Y, radius*15);
    ofCircle(X, Y, radius*10);
    ofCircle(X, Y, radius*5);

}

indent preformatted text by 4 spaces

The formatting of your reply makes it hard to see what is going on! Perhaps post your code to GitHub?

I literally love you.

1 Like

it seems that you solved it.