Reputation: 1
I am encountering an error reading, no matching conversion for functional style cast from 'int' to 'TrashCan'.
here is the declaration in header:
class TrashCan
{
friend TrashCan operator +( TrashCan& left,
TrashCan& right);
public:
TrashCan();
int size=0;
int item=0;
void setSize(int);
void addItem();
here is my implementation:
TrashCan operator +(const TrashCan& left,
const TrashCan& right) {
TrashCan t= TrashCan( left.size + right.size );
return( t );
}
here is the main with operator at bottom:
int main( ) {
cout << "Welcome to My TrashCan Program!" << endl;
TrashCan myCan;
TrashCan yourCan;
yourCan.setSize( 12 );
myCan.setSize( 12 );
yourCan.addItem( );
yourCan.addItem( );
myCan.addItem( );
myCan.printCan();
yourCan.printCan();
//TrashCan combined = yourCan + myCan;
Upvotes: 0
Views: 161
Reputation:
Edit
You declare your constructor like this: TrashCan();
But you call it like this: TrashCan t= TrashCan( left.size + right.size );
.
You need to have a second constructor like TrashCan(int nsize) : size(nsize) { }
.
godel9 already put the answer in the comments, but here's a working code example:
#include <iostream>
class TrashCan {
// Your declaration did not match your definition
// Need to put const here
friend TrashCan operator +(const TrashCan& left,
const TrashCan& right);
public:
TrashCan(int nsize) : size(nsize) { }
~TrashCan() { }
int size;
};
TrashCan operator +(const TrashCan& left,
const TrashCan& right)
{
TrashCan t= TrashCan( left.size + right.size );
return( t );
}
int main()
{
TrashCan tc1(10);
TrashCan tc2(20);
std::cout << (tc1 + tc2).size;
// outputs 30
return 0;
}
Upvotes: 3