0

I have this simple function :

DBConfig::DBConfig(const IniParser& ini) 
{
    m_url = ini.GetValue("host");
    m_user = ini.GetValue("db_user");
    m_pass = ini.GetValue("db_pass");
    m_schemaName =  ini.GetValue("db_schema"); 
}    

which i call from main

IniParser ini;
DBConfig *pDBConfig = new DBConfig(ini);

The method in the IniParser looks like this :

std::string GetValue(const std::string& key, const std::string& section = "");

when i point at the for example: m_url = ini.GetValue("host");

I'm getting :

the object has type qualifiers that are not compatible with the member function "IniParser::GetValue"C/C++(1086)
    DBConfig.cpp(6, 17): object type is: const IniParser

Why when i try to compile it I'm getting this error :

./data_access/DBConfig.cpp: In constructor ‘DBConfig::DBConfig(const IniParser&)’:
./data_access/DBConfig.cpp:6:36: error: passing ‘const IniParser’ as ‘this’ argument discards qualifiers [-fpermissive]
         m_url = ini.GetValue("host");
                                    ^
In file included from ./data_access/DBConfig.h:4:0,
                 from ./data_access/DBConfig.cpp:1:
/home/vagrant/cpp/libhv/build/include/hv/iniparser.h:31:17: note:   in call to ‘std::__cxx11::string IniParser::GetValue(const string&, const string&)’
     std::string GetValue(const std::string& key, const std::string& section = "");
Aamir
  • 1,974
  • 1
  • 14
  • 18
user63898
  • 29,839
  • 85
  • 272
  • 514
  • 4
    You need to mark `GetValue` as `const` member function to make it callable on `const` object. – songyuanyao Jan 16 '23 at 06:55
  • 1
    const class objects can only explicitly call const member functions https://www.learncpp.com/cpp-tutorial/const-class-objects-and-member-functions/ – dorKKnight Jan 16 '23 at 07:07
  • @dorKKnight what is the optimized way to pass this object without unnecessary memory allocation? – user63898 Jan 16 '23 at 07:18
  • @user63898 the IniParser class has 'GetValue' function which can be marked as 'const' ..for example the signature should look like std::string GetValue(const std::string& key, const std::string& section = "") const; ... this should solve the compilation error. – dorKKnight Jan 16 '23 at 07:47

0 Answers0