0

The following operation is part of a cash register machine.

In order to generate a bill I should count the products:
1. Each product is added once to the bill.
2. In case the product is already exist in this bill, its quantity is incremented.

void CashRegister::countProducts()
{
OMIterator<Product*> iter(itsProduct);
CountedProduct* cp;
Product* p;

// Iterate through all products counting them
while (*iter) {
  // get the next product
  p = *iter;

  // has this already been added?
  cp = getItsCountedProduct(p->getBarcode());

  // If it does not exist then add it else increment it
  if (NULL==cp) {
    addItsCountedProduct(p->getBarcode(), new CountedProduct(p));
  } else {                                                       
    cp->increment();
  }                 
  // point to next
  ++iter;
}    

and:

void CashRegister::addItsCountedProduct(int key, CountedProduct* p_CountedProduct) 
{
if(p_CountedProduct != NULL)
    {
        NOTIFY_RELATION_ITEM_ADDED("itsCountedProduct", p_CountedProduct, false, false);
    }
else
    {
        NOTIFY_RELATION_CLEARED("itsCountedProduct");
    }
itsCountedProduct.add(key,p_CountedProduct);

}

I get the following error:
error C2664: 'CountedProduct::CountedProduct' : cannot convert parameter 1 from 'Product *' to 'const CountedProduct &'

The error is references to this line:
addItsCountedProduct(p->getBarcode(), new CountedProduct(p));

Any ideas?

user3165438
  • 2,631
  • 7
  • 34
  • 54
  • You don't show your constructor for `CountedProduct`, but `p` is a `Product *` and the error seems to indicate that you only have a `CountedProduct(const CountedProduct&)`? – crashmstr Dec 03 '15 at 13:04
  • 3
    C++ is not C# or Java. Don't be mislead by similar syntax or familiar keywords, it is completely different language, programming culture and set of paradigms. Before proceeding any further, you might want to learn more about "C++ ways" first: [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) (start with best practices and intermediate sections). – Ivan Aksamentov - Drop Dec 03 '15 at 13:04

2 Answers2

2

If the function takes a const CountedProduct& then you shouldn't be creating a pointer.

addItsCountedProduct(p->getBarcode(), new CountedProduct(p))  // wrong

You should just create an instance on the stack

addItsCountedProduct(p->getBarcode(), CountedProduct(p))
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

The statement new CountedProduct(p) returns a pointer, type: CountedProduct*. You want to a constant reference: const CountedProduct &.

To fix this replace the line:

addItsCountedProduct(p->getBarcode(), new CountedProduct(p));

with:

addItsCountedProduct(p->getBarcode(), CountedProduct(p));

As an aside calling new CountedProduct(p) and not keeping the pointer to the object created will cause a memory leak. It is allocated memory on the heap and you have no way to later deallocate this memory (with a delete call).

Panda
  • 1,231
  • 18
  • 27