'C++ : What is wrong with my class constructors ? (no default constructor exists &/or conflicting declaration)?
Trying to write a simple exponential filter class, self contained in a .h file. I'm getting these errors:
- no default constructor exists for class "ExpoFilter"
- conflicting declaration 'ExpoFilter alpha_temp'
I don't understand the problem, I have 2 constructors, one for an int type parameter and one for a float type parameter. Isn't that enough to be considered 2 different function signatures?
I am also calling the first constructor from within the second one, which I've done successfully in the past. Also tried proper full .cpp + .h file without success.
So what is wrong?
Here is the code:
/*
Exponential filter, a.k.a exponential moving average, object Class
*/
#ifndef ExpoFilter_h
#define ExpoFilter_h
class ExpoFilter
{
// user-accessible "public" interface
public:
// Constructors
ExpoFilter::ExpoFilter(float alpha){
_alpha = alpha;
_alpha2 = 1.0f - _alpha;
}
ExpoFilter::ExpoFilter(int N){
float alpha_temp = 2.0f / (N + 1);
ExpoFilter(alpha_temp);
}
ExpoFilter::operator float(){
return _value;
}
float ExpoFilter::operator=(float newValue)
{
return NewValue(newValue);
}
float ExpoFilter::NewValue(float newValue){
_value = newValue * _alpha + _value * _alpha2;
return _value;
}
// library-accessible "private" interface
private:
float _alpha;
float _alpha2;
float _value;
};
#endif
Solution 1:[1]
The problem is that since you have a parameterized constructor for your class ExpoFilter, the compiler will not synthesize the default constructor ExpoFilter::ExpoFilter() by itself. This means that if you want to create/construct an object of class ExpoFilter using the default constructor, say by writing ExpoFilter alpha_temp;, then the compiler will produce error because there is no default constructor.
To solve this you must add a default constructor for your class as shown below:
class ExpoFilter
{
//other code here as before
//default constructor that uses constructor initializer list
ExpoFilter() : _alpha(0), _alpha2(0), value(0)
{
}
}
Now, when you wrote:
ExpoFilter(alpha_temp); //this creates a temporary object named alpha_temp using the default constructor
The above statement creates an object of type ExpoFilter named alpha_temp using the default constructor. But since the compiler did not synthesize a default ctor, you will get the mentioned error.
If your intention was to call the parameterized constructor instead of default constructor then you could change the above statement to:
ExpoFilter(2.0f / (N + 1));
Solution 2:[2]
"Default constructor" in this context means a constructor that takes no arguments, i.e.
ExpoFilter() {...}
You have a constructor that takes an int argument, and a constructor that takes a float argument, but no constructor that takes no arguments. So code like this:
ExpoFilter alpha_temp;
... can't compile because the compiler doesn't know how to construct the object with no argument supplied.
Btw, this line:
ExpoFilter(alpha_temp);
.... is not calling a constructor-method on your object; rather it is constructing a second (temporary/anonymous) ExpoFilter object that is created on the stack and then immediately destroyed -- probably not what you intended. (in particular, that line won't change the state of your original ExpoFilter object!). If you want to delegate to another constructor-method from within your constructor (and you are compiling with C++11 or newer) you'll need to call the other-constructor from your constructor's initializer-list instead:
ExpoFilter::ExpoFilter(int N)
: ExpoFilter(2.0f / (N + 1))
{
// ...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anoop Rana |
| Solution 2 |
