Reputation: 43
I have a C++ code, which compiles and runs fine, though I have some doubt that I need help with. I am a newbie here, so please bear with me. This is the code.
#include <iostream>
using namespace std;
//class float
class fl {
float val;
public:
fl(){ val = 0.0; }
fl(float val){ this->val = val; }
float get_val(){ return val; }
};
int main(){
fl var0, var1;
var0 = 6.0;
cout << "var0.val after " << var0.get_val() << endl;
var1 = 8.0;
cout << "var1.val after " << var1.get_val() << endl;
return 0;
}
The above code however doesn't compile and reports this error with I comment this overloaded constructor: fl(float val){ this->val = val; }
op_ovl_1.cxx: In function 'int main()':
op_ovl_1.cxx:15:12: error: no match for 'operator=' in 'var0 = 6.0e+0'
op_ovl_1.cxx:5:10: note: candidate is: fl& fl::operator=(const fl&)
op_ovl_1.cxx:17:12: error: no match for 'operator=' in 'var1 = 8.0e+0'
op_ovl_1.cxx:5:10: note: candidate is: fl& fl::operator=(const fl&)
As far as I understand, constructors are called only during initialization, not during regular assignments (Please correct me, if I am wrong). So, isn't it somewhat confusing that the constructor seems to be called for regular assignments?
Upvotes: 3
Views: 101
Reputation: 7147
When writing classes, you should obey the "rule of three" - this means: write a destructor, copy constructor and an assignment operator.
Your class is missing a copy constructor and an assignment operator!
A better implementation of your class:
//class float
class fl {
float val;
public:
fl() : val(0.0f) {}
fl( float val ) : val(val) {}
// copy constructor
fl( const fl& other ) : val(other.val) {}
// assignment operator
fl& operator=( const fl& other ) { val=other.val; return *this; }
float get_val() const { return val; }
};
Please note some other goodies, I added here ;-)
Besides this, float constants should always have an "f" appended. Otherwise they are interpreted as of type double
.
Upvotes: 1
Reputation: 5002
You are trying to assign a double to an object of type f1. You can correct it by using a function that changes the value of val or by var0.val = 6.0.
The error you are receiving is because C++ allows you to modify what happens when you use different operators. Like in your case or the solution trojanfoe proposed he is modifying the behavior of your class when it uses the '=' operator.
Upvotes: 0
Reputation: 206689
If you leave the fl(float val)
constructor, the compiler can convert those float constants to fl
objects, then assign that to the varX
variables.
That constructor is not used on the varX
variables, but on the constants on the right hand side of the assignment.
Upvotes: 3
Reputation: 17622
It is trying to call the default assignment operator in your fl
class, and fails because that operator expects the right-hand value to be the same type as the assignee (in this case, fl
). You can either call an actual constructor and pass a float value to it, or define your own assignment operator (like trojanfoe has suggested in his answer). I recommend the former option though.
Upvotes: 1