Jason Miesionczek
Jason Miesionczek

Reputation: 14448

Passing something as this argument discards qualifiers

Using the below code, i get the following compile error:

In static member function ‘static std::string ctedata::Record::getDispatcher<std::basic_string<char> >::impl(const ctedata::Record&, const string&)’:
/home/jason/CrownTheEmpire/lib/ctedata/data.h:111:38: error: passing ‘const std::map<std::basic_string<char>, std::basic_string<char> >’ as ‘this’ argument discards qualifiers [-fpermissive]
         return rec.fieldValues_[field];
                                      ^
In file included from /usr/include/c++/5.1.0/map:61:0,
                 from /usr/include/cppconn/connection.h:30,
                 from /usr/include/cppconn/driver.h:30,
                 from /home/jason/CrownTheEmpire/lib/ctedata/data.h:6,
                 from /home/jason/CrownTheEmpire/lib/ctedata/data.cc:3:
/usr/include/c++/5.1.0/bits/stl_map.h:471:7: note:   in call to ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::basic_string<char>; _Tp = std::basic_string<char>; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::basic_string<char>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::basic_string<char>]’
       operator[](const key_type& __k)
       ^

class Record
{
  public:
    Record() {
        isNew_ = true;
    }
    Record(sql::ResultSet *resultSet) : resultSet_(resultSet) {
        isNew_ = false;
    }

    void loadData();
    virtual void init() = 0;

    bool isNew() { return isNew_; }
    void setValue(const std::string& field, const std::string& value);
    template<typename T>
    T getValue(const std::string& field) const {
        return Record::getDispatcher<T>::impl(*this, field);
    }
  protected:
    sql::ResultSet* resultSet_;
    bool isNew_;
    std::map<std::string, DataTypes> fieldDataTypes_;
    std::map<std::string, std::string> fieldValues_;
  private:
    template<typename T>
    struct getDispatcher;
};

template<>
struct Record::getDispatcher<std::string> {
    static std::string impl(Record const& rec, std::string& const field) {
        return rec.fieldValues_[field];
    }
};

template<>
struct Record::getDispatcher<int> {
    static int impl(Record const& rec, const std::string& field) {
        return 0;
    }
};

I think i'm doing things correctly, but still getting this error that i can't figure out. Can anyone see where i've gone wrong? Thanks.

Upvotes: 0

Views: 212

Answers (1)

Jarod42
Jarod42

Reputation: 217740

There are no operator[] of std::map which is const, you have to use at or find:

template<>
struct Record::getDispatcher<std::string> {
    static std::string impl(Record const& rec, std::string& const field) {
        return rec.fieldValues_.at(field); // throw if field is not in map.
    }
};

or

template<>
struct Record::getDispatcher<std::string> {
    static std::string impl(Record const& rec, std::string& const field) {
        auto it = rec.fieldValues_.find(field);
        if (it == rec.fieldValues_.end()) {
            // not found.
            // Manage the case, return default value or throw.
        } else {
            return it->second;
        }
    }
};

Upvotes: 2

Related Questions