jeudi 5 mai 2016

error: passing ‘constas ‘this’ argument discards qualifiers [-fpermissive]

I have a singleton class:

class ObjectModel {
public:
    virtual ~ObjectModel();
    static ObjectModel& Get();

    const AttributeStruct operator [] (const std::string &symbol_path) const {
        auto path = split(symbol_path, '.');

        if (path.size() != 2) {
            throw std::runtime_error(Formatter() << "Path '" 
               << symbol_path << "' has a wrong format.");
        }

        const CipAttributeStruct attr = this->dic[path[0]][path[1]];
        if (attr.data == nullptr) {
            throw std::runtime_error(Formatter() << "Attribute '" 
               << symbol_path << "' isn't found.");
        }

        return attr;
    }
private:
    ObjectModel();
    ObjectModel( const ObjectModel& );
    ObjectModel& operator=( ObjectModel& );

    std::map<std::string, std::map<std::string, CipAttributeStruct> > dic = {};
};

Usage:

void main() {
    auto &m = ObjectModel::Get();

    std::cout << *static_cast<uint32_t*>(m["Object1.Attribute"].data);
}

I can't compile this code due to an error:

error: passing ‘const std::map, std::map, lynx::cip::CipAttributeStruct> >’ as ‘this’ argument discards qualifiers [-fpermissive] const CipAttributeStruct attr = this->dic[path[0]][path[1]];

I see lots questions of this kind here, but none of them helps me. I know I can remove const qualifier to avoid the problem. But I wonder if there is another way.

Aucun commentaire:

Enregistrer un commentaire