I've been having a really hard time finding the correct way to overload the += operator. I used what seems to be the most popular method of doing it but it doesn't seem to meet the needs of this program. If anyone can help me solve this compiler error or point me in the right direction that would be amazing. Here's the code.
Problem function...
////////////storage.cpp
void storeItems( istream &inf, vector<ItemStack> &items ){
int id = 0; //temporary id
int q = 0; //temporary count
cout << "Processing Log" << "\n";
printHorizontalLine( cout, '-', 36 );
while( inf >> id >> q ){
int loc = sequentialSearch( items, id );
if( loc != -1 ){
items[loc] += q; <------ This operator is causing the error.
cout << " Stored "
<< right << setw(3) << q << " "
<< items[loc].getName()
<< "\n";
}
else{
cout << " Invalid ID (" << id << ")" << "\n";
}
}
println();
}
///////////itemstack.cpp
ItemStack::ItemStack( int i, std::string n ){
id = i;
name = n;
quantity = 0;
}
/**
*
*/
//void ItemStack::add( int amount ){
// quantity += amount;
//}
inline
ItemStack& ItemStack::operator+= (const ItemStack &rhs)
{
quantity+= rhs.quantity;
return *this;
}
inline
ItemStack operator+(ItemStack lhs, const ItemStack& rhs)
{
lhs += rhs;
return lhs;
}
/**
*
*/
bool ItemStack::lowSupply(){
// Note the similarity to a condition in an if statement
return (quantity < 10);
}
bool ItemStack::operator== (const ItemStack& s1) const
{
return id == s1.id
&& quantity == s1.quantity
&& name == s1.name;
}
bool ItemStack::operator< (const ItemStack& s1)
{
return id == s1.id;
}
inline
ostream& operator<<( std::ostream &outs, const ItemStack &prt )
{
return outs;
}
`