Store.cpp:
table[plu_code] = new Product(plu_code, name, sold_by_weight, price, inv);//This will call look up table operator '[]'
num_products++; // counting number of items in inventory
LookupTable.h:
private:
T *aptr[MAXRANGE];
int rangeStart[MAXRANGE];
int rangeEnd[MAXRANGE];
int numRanges;
T defaultValue; //needed to return when [] operator does not find valid product
T &LookupTable<T>::operator[](int value)
{
// find the range
if (value>0 && value<9999) //inorganic tems
return aptr[0][value-rangeStart[0]];
else if (value>90000 && value<99999)//organic items
return aptr[1][value - rangeStart[1]];
else
return defaultValue;
}
问题是当返回指针时,它无法为产品对象分配地址。