c++11constructor

Got "expected type specifier" error while instantiating my property


My goal is to create nullable value which takes both a default value function and a key to retrieve a potential customization of the value from the flash storage.

But trying to instantiate it as a property member I get "expected type specifier" compile error for the two arguments.

How to fix this?

Here how I use my constructor in my class:

class BatteryConfig {
    static float _defaultPackL1Undervoltage();

    FloatStorageNullableWithDefault packL1Undervoltage("PackL1UndervoltageKey", _defaultPackL1Undervoltage); // <<<== the errors here
};

Here is my base classes:

template <typename T>
class Nullable {
public:
    Nullable() : hasValue_(false) {}
    Nullable(const T& value) : hasValue_(true), value_(value) {}

    bool hasValue() const { return hasValue_; }

    virtual T get() const {
        if (!hasValue_) {
            throw std::runtime_error("Nullable value is not set");
        }
        return value_;
    }

    virtual void set(const T& value) {
        hasValue_ = true;
        value_ = value;
    }

protected:
    bool hasValue_;
    T value_;
};

template <typename T>
class NullableWithDefault : public Nullable<T> {
public:
    NullableWithDefault(const std::function<T()>& defaultValue) : Nullable<T>(), defaultValue_(defaultValue) {}

    T get() const override {
        if (!Nullable<T>::hasValue_) {
            return defaultValue_();
        }
        return Nullable<T>::get();
    }

protected:
    std::function<T()> defaultValue_;
};


class FloatStorageNullableWithDefault : public NullableWithDefault<float> {
public:
    FloatStorageNullableWithDefault(
        const std::string& key,
        const std::function<float()>& defaultValue
    ) : NullableWithDefault<float>(defaultValue), key_(key) {}

    /// returns the key
    std::string getKey() const { return key_; }

private:
    const std::string key_;
};

Solution

  • I see you're trying to create a nullable value with a default value function and a key to retrieve a potential customization of the value from the flash storage. However, you're getting a compile error when trying to instantiate it as a property member.

    The issue is that you're trying to initialize a non-static member variable with a non-constant expression. In C++, you can't initialize a non-static member variable with a function call or a non-constant expression.

    To fix this, you can use an initializer list in the constructor of your BatteryConfig class. Here's the corrected code:

        class BatteryConfig {
    public:
        BatteryConfig() : packL1Undervoltage("PackL1UndervoltageKey", _defaultPackL1Undervoltage) {}
    
        FloatStorageNullableWithDefault packL1Undervoltage;
    
        static float _defaultPackL1Undervoltage() { /* implementation */ }
    };
    

    By using an initializer list, you're ensuring that the packL1Undervoltage member is initialized with the correct arguments when the BatteryConfig object is constructed.

    Alternatively, you can also use a default member initializer, which is a new feature in C++11:

        class BatteryConfig {
    public:
        FloatStorageNullableWithDefault packL1Undervoltage = FloatStorageNullableWithDefault("PackL1UndervoltageKey", _defaultPackL1Undervoltage);
    
        static float _defaultPackL1Undervoltage() { /* implementation */ }
    };
    

    This way, you can initialize the packL1Undervoltage member with the default value without needing an initializer list in the constructor.