ich eine Singleton-Klasse haben:Fehler: ‚Constas 'this' vorbei Argument verwirft Qualifier [-fpermissive]
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 = {};
};
Verbrauch:
void main() {
auto &m = ObjectModel::Get();
std::cout << *static_cast<uint32_t*>(m["Object1.Attribute"].data);
}
ich diesen Code nicht aufgrund eines kompilieren Fehler:
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]];
Ich sehe viele Fragen dieser Art hier, aber keiner von ihnen hilft mir. Ich weiß, dass ich const Qualifier entfernen kann, um das Problem zu vermeiden. Aber ich frage mich, ob es einen anderen Weg gibt.