4 Replies - 1710 Views - Last Post: 06 December 2008 - 01:09 PM Rate Topic: -----

#1 puzzler5809  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-April 08

trying to move function to class

Posted 30 November 2008 - 01:58 PM

I am trying to move the following function to cashRegister class and can't seem to do it. I keep getting errors from the complier such as member function not defined. I know it has to do with dispenserType and cashRegister, just not sure how to fix it.

Below is the code from the main program.
void sellProduct(dispenserType& product, cashRegister& pCounter)
{
	int amount;  //variable to hold the amount entered
	int amount2; //variable to hold the extra amount needed

	if (product.getNoOfItems() > 0) //if the dispenser is not 
									//empty
	{
		cout << "Please deposit " << product.getCost()
			 << " cents" << endl;
		cin >> amount;

		while (amount < product.getCost())
		{
			cout << "Please deposit another "
				 << product.getCost()- amount
				 << " cents" << endl;
			cin >> amount2;
			amount = amount + amount2;
	   }

		if (amount >= product.getCost())
		{
			pCounter.acceptAmount(amount);
			product.makeSale();
			cout << "Collect your item at the bottom and "
				 << "enjoy." << endl;
		}
		

		cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
			 << endl << endl;
	}
	else
		cout << "Sorry, this item is sold out." << endl;
}//end sellProduct



I attempted to change the function under candyMachineImp.cpp to:
void cashRegister::sellProduct(dispenserType& product, cashRegister& pCounter)



with the definition under class cashRegister as:
   void sellProduct(dispenserType&, cashRegister&);



Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: trying to move function to class

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: trying to move function to class

Posted 30 November 2008 - 02:24 PM

Not clear to me what the problem is (perhaps posting the actual errors would be helpful), but it seems if sellProduct is a method of the cashRegister class, you shouldn't need to pass a cashRegister object to it any longer.

This post has been edited by JackOfAllTrades: 30 November 2008 - 02:25 PM

Was This Post Helpful? 1
  • +
  • -

#3 puzzler5809  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-April 08

Re: trying to move function to class

Posted 30 November 2008 - 07:42 PM

I tried it as you suggested by removing the cashRegister objcet.
I now have candyMachine.h as:
class cashRegister
{
	  public:
			 int getCurrentBalance() const;
			 void acceptAmount(int amountIn);
			 void showSelection();
			 void sellProduct(dipsenserType&);
			 cashRegister(int cashIn = 500);
			 
	  private:
			  int cashOnHand;
};

class dispenserType
{
	  public:
			 int getNoOfItems() const;
			 int getCost() const;
			 void makeSale();
			 dispenserType(int setNoOfItems = 50, int setCost = 50);
	  
	  private:
			  int numberOfItems;
			  int cost;
};


candyMachineImp.cpp is now coded as:
void cashRegister::sellProduct(dispenserType& product)


The errors I am getting are:
11 candyMachine.h variable or field `sellProduct' declared void
11 candyMachine.h expected `;' before '(' token
26 candyMachineImp.cpp no `void cashRegister::sellProduct(dispenserType&)'
member function declared in class `cashRegister'
Makefile.win [Build Error] [candyMachineImp.o] Error 1

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: trying to move function to class

Posted 01 December 2008 - 03:47 AM

Decide how you want to type 'dispenser' and do it the same everywhere.
Was This Post Helpful? 0
  • +
  • -

#5 puzzler5809  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-April 08

Re: trying to move function to class

Posted 06 December 2008 - 01:09 PM

Thanks to all who helped. The problem turned out to be the placement of the definition. Once I moved class cashRegister after class dispenserType, it worked. Yeah, on to my final project.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1