Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading