I have classes Pile which represents a card deck, and so contains instances of class Card.
I have overloaded two operators, Pile::operator+= and Pile::operator–.
int main()
{
Pile pile1(false); //construct an empty pile/deck
Pile pile2(true); //construct a full deck with all 52 cards
output(pile1,pile2); //just a little procedure to print both decks
pile1 += --pile2;
output(pile1,pile2);
...
Operator += takes another Pile as a reference and moves every card from the parameter Pile to *this.
Operator — takes the topmost card from the pile and returns a Pile containing that one card.
What g++ is giving me is a compile-time error stating
error: no match for 'operator+=' in 'pile1 += Pile::operator--()()'
note: candidate is: void Pile::operator+=(Pile&)
Below are the overloaded operators:
void Pile::operator+=(Pile &other)
{
Node *n = other.listHead;
//add each card from other to *this
while((n = n->getNext()) != NULL)
{
this->newCardToList(other.drawCard());
}
}
Pile Pile::operator--()
{
Pile pile(false);
pile.newCardToList(this->drawCard());
return pile;
}
To me it looks like operator+= is trying to take the — as a parameter.
I tried pile1 += (–pile2); but that didn’t change anything.
The thing I want (or need to do here) is to take the topmost card from pile2
and put it to pile1. Could you suggest what is wrong here, as I haven’t been able to come up with anything?
Edit:
The += is required to modify both objects here. This is required because the person who gave us this exercise project on our course requires us to do it this way. His design has been really bad though, so I wouldn’t be surprised if this solution wasn’t even possible.
operator += is not recognized because operator+= correct signature is:
If you want to modify
otherinside, you can useand