-2

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;
 }

`

Zivian
  • 1
  • 2
  • Your operator is fine, it's just *how* you use is wrong. – Rakete1111 Jul 27 '16 at 01:40
  • So, what did you expect would happen at `items[loc] += q`? Did you expect `q` to get implicitly converted to `ItemStack` type? Or something else? – AnT stands with Russia Jul 27 '16 at 01:49
  • If I define += as taking an int as a parameter I get an undefined reference to 'ItemStack::operator+=(int const&) error. I'm having a hard time getting a variable that isn't in the class added to a class variable that's already an int using this overloaded operator. – Zivian Jul 27 '16 at 02:04
  • @Zivian Please show your updated code that fails. Alternatively, add a constructor to `ItemStack` that takes a single `int` as input. Then you can pass an `int` to `operator+=(const ItemStack &)` and it will be implicitly converted to an `ItemStack` for you. – Remy Lebeau Jul 27 '16 at 02:24
  • Your code is incomplete; in particular, it seems to be missing a `main()` function and at least one `#include`. Please [edit] your code so it's a [mcve] of your problem, then we can try to reproduce and solve it. You should also read [ask]. – Toby Speight Jul 28 '17 at 12:23

1 Answers1

2

Your operator+= is defined as

ItemStack& ItemStack::operator+= (const ItemStack &rhs) 

The operator+= is defined for an ItemStack & parameter.

You are attempting to use it as follows:

items[loc] += q;

q is an int, and not an ItemStack. Your += operator is defined only for an ItemStack reference.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • So to add an integer to an object of item stack, you'd think I would just change the parameter to int instead of an ItemStack reference. I get an undefined reference error when I do that though. – Zivian Jul 27 '16 at 02:13
  • Making random code changes, without an actual understanding of how operator overloading works, is unlikely to produce the desired results. – Sam Varshavchik Jul 27 '16 at 02:15