Thread: issue with member functions

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    issue with member functions

    I have a bag:
    Code:
    class bag
        { 
        public:
            // TYPEDEFS and MEMBER CONSTANTS
    		typedef Die value_type;
            typedef std::size_t size_type;
            static const size_type CAPACITY = 30;
            // CONSTRUCTOR
            bag( ) { used = 0; } 
            // MODIFICATION MEMBER FUNCTIONS
            size_type erase(const value_type& target);
            bool erase_one(const value_type& target);
            void insert(const value_type& entry);
            void operator +=(const bag& addend);
            // CONSTANT MEMBER FUNCTIONS
            size_type size( ) const { return used; }
            size_type count(const value_type& target) const;
            value_type getItem(int element) const;
        private:
            value_type data[CAPACITY];  // The array to store items
            size_type used;             // How much of array is used
        };
    and I am trying to call the roll() function of the die class through a bag object's getItem() function, getItem simply returns data[element] (a single die in this case)...however, using this line of code:
    Code:
    //Roll the dice in rollingDice and add face value to sumFace
    	for (int i = 0; i < numRoll; ++i)
    	{
    		rollingDice.getItem(i).roll();
    		cout << rollingDice.getItem(i).getFace();
    		//sumFace = sumFace + rollingDice.getItem(i).getFace();
    	}
    does not alter the face value at all, here's roll():
    Code:
    void Die::roll()
    {
    	int randNum = genRand(1, numSides);
    	setFace(randNum);
    }
    and I am completely confused as to why the face value of the Die within the Bag is not being altered...any idea why this is happening?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    How is setFace defined/declared (did you make the Die class?)

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    Yeah, I made the Die class, and the function looks like this

    Code:
    void Die::setFace(int f)
    {
    	faceVal = f;
    }
    Thanks for the response by the way

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> value_type getItem(int element) const;
    getItem is returning a copy of the die. If you want that object to be changed, you should return a reference. You should have two getItem functions, one const one returning a const reference, one non-const one returning a non-const reference.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    wow, that is the most subtle bug I have ever encountered...I was return a copy of the Die object in getItem()...so it was changing the faceVal of the copy and then the original was unchanged. Jesus, that took me an hour or so to find that...Thanks for the reply...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Callbacks to member functions
    By prog-bman in forum C++ Programming
    Replies: 0
    Last Post: 01-19-2008, 02:48 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Array of member functions
    By Ward in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2006, 07:47 AM
  4. Some help with class member functions and pointers
    By k_rock923 in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2005, 12:28 AM
  5. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 03:52 PM