Thread: How to you use values from other function in the same class?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    How to you use values from other function in the same class?

    Code:
    #include "Shapes.h"
    #include "Global.h"
    
    
    #include <iostream>
    #include <SFML/Graphics.hpp>
    
    
    Shapes::Shapes()
    {
        //ctor
    }
    
    
    Shapes::~Shapes()
    {
        //dtor
    }
    
    
    void Shapes::setShapes()
    {
        MainButton1.setSize(sf::Vector2f(100,80));
        MainButton1.setPosition(sf::Vector2f( ScreenWidth/ 10 , ScreenHeight / 4 ));
        MainButton1.scale(2,1);
        MainButton1.setFillColor(sf::Color::White);
        MainButton1.setOutlineColor(sf::Color::Black);
        MainButton1.setOutlineThickness(3);
    
    
        MainButton2.setSize(sf::Vector2f(100,80));
        MainButton2.setPosition(sf::Vector2f( ScreenWidth/ 10 , ScreenHeight / 2.5 ));
        MainButton2.scale(2,1);
        MainButton2.setFillColor(sf::Color::White);
        MainButton2.setOutlineColor(sf::Color::Black);
        MainButton2.setOutlineThickness(3);
    
    
        MainButton1RED.setSize(sf::Vector2f(100,80));
        MainButton1RED.setPosition(sf::Vector2f( ScreenWidth/ 10 , ScreenHeight / 4 ));
        MainButton1RED.scale(2,1);
        MainButton1RED.setFillColor(sf::Color::Yellow);
        MainButton1RED.setOutlineColor(sf::Color::Black);
        MainButton1RED.setOutlineThickness(3);
    
    
        MainButton2RED.setSize(sf::Vector2f(100,80));
        MainButton2RED.setPosition(sf::Vector2f( ScreenWidth/ 10 , ScreenHeight / 2.5 ));
        MainButton2RED.scale(2,1);
        MainButton2RED.setFillColor(sf::Color::Yellow);
        MainButton2RED.setOutlineColor(sf::Color::Black);
        MainButton2RED.setOutlineThickness(3);
    
    
    }
    
    
    void Shapes::ChangeButtonColor( int buttonNum )
    {
        if( buttonNum == 1) // this in main
        {
            buttonCOLOR = 1;
            setButtonColor(buttonCOLOR);
        }
        else if ( buttonNum == 2) // this in main
        {
            buttonCOLOR = 2;
            setButtonColor(buttonCOLOR);
        }
        else if ( buttonNum == 0) // this in main
        {
            buttonCOLOR = 0;
            setButtonColor(buttonCOLOR);
        }
    
    
    }
    
    
    int Shapes::setButtonColor(int buttonColor)
    {
        this->buttonCOLOR = buttonCOLOR;
        return buttonCOLOR;
    }
    
    
    void Shapes::Draw(sf::RenderWindow &VoteApp)
    {
    
    
        if( 0 == buttonCOLOR )
        {
            VoteApp.draw(MainButton1);
            VoteApp.draw(MainButton2);
            std::cout << "should paint the two buttons white" << std::endl;
        }
        else if( 1 == buttonCOLOR)
        {
            VoteApp.draw(MainButton1RED);
            VoteApp.draw(MainButton2);
            std::cout << "should paint the button 1 yellow" << std::endl;
        }
        else if( 2 == buttonCOLOR )
        {
            VoteApp.draw(MainButton2RED);
            VoteApp.draw(MainButton1);
            std::cout << "should paint the button 2 yellow" << std::endl;
        }
    
    
        std::cout << "buttonColor: " << buttonCOLOR << std::endl;
    }
    I don't understand why it print out buttonColor : 72, instead of the value from the Shapes::setButtonColor(int buttonCOLOR) function. How do I make the buttonCOLOR from Shapes::ChangeButtonColor(int buttonNum) to have the same value in the Shapes::raw(sf::RenderWindow &VoteApp) function?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Code:
    int Shapes::setButtonColor(int buttonColor)
    {
        this->buttonCOLOR = buttonCOLOR;
        return buttonCOLOR;
    }
    Check your capitalization. Your method parameter is never used. Instead you assign your field to the same field
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    then how?? I still have no clue..

  4. #4
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    I guess I'll just have to put another parameter to all functions and make it work.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In setButtonColor you passed in an integer named "buttonColor" but don't use it. buttonColor is not the same as buttonCOLOR, but this does seem to be the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using values from derived class in base class
    By gratuitous_arp in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2011, 09:25 AM
  2. need help printing some values from a queue class.
    By newbc in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2011, 07:47 PM
  3. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  4. Displaying values from one class in another...
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2006, 07:36 AM
  5. casting c++,const in class types,function return values...
    By ABitLazy in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2004, 03:44 PM