2015-06-22 34 views
0

使用下面的代碼,我得到以下編譯錯誤:傳遞的東西,因爲這說法丟棄預選賽

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; 
    } 
}; 

我想我正確地做事情,但還是收到這個錯誤,我想不通。任何人都可以看到我出錯了嗎?謝謝。

回答

2

沒有的std::mapoperator[]這是const,你必須使用atfind

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. 
    } 
}; 

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; 
     } 
    } 
}; 
+0

完美,謝謝。 –

相關問題