badri
badri

Reputation: 627

Issue when using property_tree for a ini file

Strange error when compiling with property tree to write and read a ini (text) file. Simple standalone program(MSVP) works fine. But when i include it in my main code, I get this error. What could this mean ?

It looks like it is not happy with me including

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>

When I comment them out, this error goes away. I have -lboost_system in my LD_LIBS. Would ptree come under boost_system library or could it be any other library that I should include ?

In file included from /home/badri/usr/include/boost/property_tree/ptree.hpp:516,
                 from recorder_apis.h:15,
                 from recorder_apis.cpp:1:
/home/badri/usr/include/boost/property_tree/detail/ptree_implementation.hpp: In member function ‘boost::property_tree::basic_ptree<K, D, C>& boost::property_tree::basic_ptree<Key, Data, KeyCompare>::get_child(const path_type&)’:
/home/badri/usr/include/boost/property_tree/detail/ptree_implementation.hpp:571:58: error: declaration of ‘path’ shadows a global declaration [-Werror=shadow]
  571 |         basic_ptree<K, D, C>::get_child(const path_type &path)
      |                                         ~~~~~~~~~~~~~~~~~^~~~
In file included from /home/badri/usr/include/boost/property_tree/ptree.hpp:15,
                 from recorder_apis.h:15,
                 from recorder_apis.cpp:1:
/home/badri/usr/include/boost/property_tree/ptree_fwd.hpp:89:67: note: shadowed declaration is here
   89 |     typedef string_path<std::string, id_translator<std::string> > path;
      |                                                                   ^~~~

I am only using the boost library. So I don't deal with the files ptree_fwd.hpp or ptree_implementation.hpp directly in anyway.

MSVP that works fine

#include<iostream>
#include<boost/property_tree/ptree.hpp>
#include<boost/property_tree/ini_parser.hpp>

using namespace std;
int main()
{
        using boost::property_tree::ptree;
        ptree pt;

        std::string jobToken1="123";
        std::string jobToken2="234";
        std::string jobToken3="345";

        pt.put("General.Token", "rec1");
        pt.put("General.Location", "/media/sd1/abc_1");

        pt.put(jobToken1+".startTime", 123456);
        pt.put(jobToken1+".endTime", 345678);

        pt.put(jobToken2+".startTime", 123456);
        pt.put(jobToken2+".endTime", 345678);

        pt.put(jobToken3+".startTime", 123456);
        pt.put(jobToken3+".endTime", 345678);


        write_ini("input.txt", pt);

        read_ini("input.txt", pt);

        for(auto& section : pt)
        {
                cout << "[" << section.first << "]\n";
                for (auto& key: section.second)
                        cout << key.first << "=" << key.second.get_value<string>() <<"\n";
        }
}
~    

Upvotes: 1

Views: 304

Answers (1)

sehe
sehe

Reputation: 393174

Something defines path in the surrounding scope. The error message tells you this:

ptree_implementation:57.hpp: In member function ‘basic_ptree<...>& basic_ptree<...>::get_child(const path_type& path)’:
error: declaration of ‘path’ shadows a global declaration [-Werror=shadow]

A simple repro would be e.g.

Live On Coliru

namespace boost::property_tree {
    int path() { return 0; }
}

#include <boost/property_tree/ptree.hpp>

What Gives?

The weird thing is that the definition seems to be fine, and appears for me on line 89 of ptree_fwd as well, so something else must be afoot.

Perhaps there is a rare compiler issue (only likely if your compiler is very old). Otherwise, there is likely another preprocessor issue interfering. Find out by viewing the preprocessed source (cmake -build test.cpp.i or g++ .... -E). You can upload that to a pastebin site if you want me to look at it to understand the problem.

Simplify

Apropos of nothing, here's a simplified version of your code: Live On Coliru

#include <boost/property_tree/ini_parser.hpp>
#include <iostream>

int main() {
    using boost::property_tree::ptree;
    ptree pt;
    pt.put("General.Token", "rec1");
    pt.put("General.Location", "/media/sd1/abc_1");

    for (ptree::path_type p : {"123", "234", "345"}) {
        pt.put(p / "startTime", 123456);
        pt.put(p / "endTime",   345678);
    }

    write_ini(std::cout, pt);
}

Prints

[General]
Token=rec1
Location=/media/sd1/abc_1
[123]
startTime=123456
endTime=345678
[234]
startTime=123456
endTime=345678
[345]
startTime=123456
endTime=345678

Upvotes: 1

Related Questions