Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried so hard but every time I do the problem, I at last get same unusual irritating messages. Please help me out!!! I will appreciate each and every suggestion...Thank you Patrice ......
-[Error] no matching function for call to 'Inventory::Inventory()
-I am trying to use Inventory class inside CashRegister class, but programme is not running.
-It should have printed total cost from cost of Inventory class multiplied by quantity of CashRegister class.

What I have tried:

C++
#include<iostream>
using namespace std;

class Inventory
{
	private:
		int quantity;
		double cost;
	public:
		Inventory(int q, double c){
			quantity = q;
			cost = c;
		}
		~Inventory(){
			cout<<"Quantity destroyed";
		}
		
		int getQuantity(){
			return quantity;
		}
		
		int getCost(){
			return cost;
		}
};

class CashRegister
{
	private:
		Inventory obj;
		int item, quantity;
		double total;
	public:
		CashRegister(Inventory object, int i, int q){
			obj = object;
			item = i;
			quantity = i;
			total = quantity*object.getCost();
		}
		double getTotal(){
			return total;
		}
};

int main()
{
	Inventory i(10, 11.5);
	
	int item, quantity;
	cout<<"Enter item and quantity:\n";
	cin>>item>>quantity;
	
	CashRegister c(i, item, quantity);
	cout<<"Total = "<<c.getTotal();	
}
Posted
Updated 3-Jul-19 21:16pm
v3
Comments
Patrice T 3-Jul-19 11:59am    
you forgot to tell us:
- the famous irritating messages.
- what it is supposed to do.
- what is not done as expected.

1 solution

In your CashRegister class you are trying to instantiate an Inventory object with the statement:
C++
Inventory obj;

But in your Inventory class the only constructor for it is
C++
Inventory(int q, double c)

So the two do not match. It would be better to declare the object in the CashRegister as a pointer, so you can pass the created object in your CashRegister constructor call in main.
C++
class CashRegister
{
	private:
		Inventory* obj;
		int item, quantity;
		double total;
	public:
		CashRegister(Inventory* object, int i, int q){

// ...


int main()
{
	Inventory i(10, 11.5);
	
	int item, quantity;
	cout<<"Enter item and quantity:\n";
	cin>>item>>quantity;
	
	CashRegister c(&i, item, quantity);


Note: The name object is often a reserved word or class name in OOP languages, so it is best avoided as a variable name.
 
Share this answer
 
Comments
Member 14520059 3-Jul-19 13:43pm    
Thank you Richard... I am very thankful to you. It works!!!! If there is any other way, then I will accept that..
Richard MacCutchan 4-Jul-19 3:41am    
It's an easy mistake that many of us have made in the past.
CPallini 3-Jul-19 15:49pm    
5.
Maciej Los 4-Jul-19 3:19am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900