+ 1
Please help with operator overloading
https://code.sololearn.com/cEBhsKHHR3yy/?ref=app I think this code doesnt run because of the operator-() I dont get how to overload the operator-() const Please someone help me
9 ответов
+ 5
There is a bit more wrong than just the operator-().
---
You have 3 constructors declared, but only 1 is defined:
CDAccountV2(string name); <-- defined
CDAccountV2(double won); <-- no definition for this one
CDAccountV2(); <-- no definition for this one either
Secondly your constructor that is defined isn't using the name to initialize it's member variable(s).
Instead I suggest that you define 2 constructors
CDAccountV2(); For default constructions and
CDAccountV2( const string& name, double won ); 1 constructor to initialize name AND won.
On a side note, you really want to take string as a const reference instead of a copy because these objects are expensive to copy.
---
---
istream& getline(string acnt2name, streamsize n);
This is a function declaration, it just tells the compiler that a function exists.
I think you want to use std::getline( std::cin, acnt2name ); instead.
---
---
Inside
istream& operator >>(...)
cin >> account.name;
should be
inputStream >> account.name;
---
---
In your setters ( setName and setBalace ) you are creating new, local variables. These shadow the member variables so you actually write to these temporary variables instead.
string name = newname; <-- Remove the 'string'
double balance = won; <-- Remove the 'double'
---
---
Inside CDAccountV2::operator+
You have 2 return statements, only the first 1 is ever executed.
Instead you probably want to implement the constructors I mentioned earlier so that you can initialize both variables in 1 constructor.
Another thing you can do is use the default constructor and then set each member variable individually with their respective setters before returning.
Also, you should return CDAccountV2 instead of const CDAccountV2 here because const is not enforced here.
---
---
CDAccountV2::operator-() is a unary - overload( e.g. i = 5; -i = -5 )
So you should probably return CDAccountV2( name, -balance ) instead of 0.
---
+ 2
~ swim ~
What I mean is that
class T
{
public:
T(){}
const T operator+( const T& ) const { return T(); }
};
T a;
T b;
T c = a + b;
Still compiles, despite it returning a const T, c is not const.
If it was a const T& then yea, it would matter.
Also I realize that const T does matter if you chain it with other methods, but then again why would you do that with a temporary?
+ 2
I have not gone through the complete code but I saw you + operator overloaded.
The mistake I noticed is you cannot have multiple returns from a same function.
This is how the concept of operator overloading works:
Class data
{
Data operator+(const data b);
Private:
Int Integer;
Float Decimal;
}
Call to the overloaded function will be as follows:
Data A;
Data B;
A=A+B;
So now the definition of the overloaded function would be as follow:
Data operator+(const data b)
{
This->Integer= This->Integer + b.Integer;
This->Decimal= This->Decimal + b.Decimal;
Return this;
}
Where (This) is a pointer pointing to the object making the function call. ( In our case object A)
+ 2
Chinmay Kotpalliwar
You probably want to create a new Data object inside operator+ instead of basically doing the job of the operator+= as well and return the new object instead of *this ( you forgot the * ).
You don't want that side effect, that's probably why the function is const usually.
I'm also just gonna assume you forgot the & in the parameter.
+ 1
i editted the coding on desktop visual studio based on your advice, and finally the program ran without errors .thank you so much Dennis !
0
Dennis
Yes you are right but I didn't wanted to do that way as this would be easier to understand