runefist
runefist

Reputation: 613

C++ ~ initial value of reference to non-const must be an lvalue

Yes I have checked other stackoverflow results: C++ initial value of reference to non-const must be an lvalue

ERROR: initial value of reference to non-const must be an lvalue

Call:

Value& c = getConfigValue("test");

Function:

Value* getConfigValue(const char* name) {
    if (!cleverconfig_loaded) {
        readConfig();
        if (!cleverconfig_loaded) {
            return NULL;
        }
    }
    if (!cleverconfig.HasMember(name)) {
        return NULL;
    }
    return cleverconfig[name];
}

So even after I made the parameter "name" a constant value, it still gives me this error, does anyone know why?

Upvotes: 2

Views: 15091

Answers (1)

virgesmith
virgesmith

Reputation: 822

The function returns a pointer, which you are trying to bind to a reference. This won't work. How to fix depends on what the return type of cleverConfig.operator[] is - either change the return type of the function from Value* to const Value&, or return *cleverconfig[name];

Upvotes: 1

Related Questions