I'm using boost::property_tree::ptree
and parse_ini
to read an ini file. Using ptree::iterator
I'm getting the ini sections and want to use them to create another object.
I have an object called First
that gets First(int& i, string& str)
So I'm trying to build new object like that using the return values I get from the ptree functions for example (posision
is my ptree::iterator
)
First* one = new First(
boost::lexical_cast<int>((posision->second).get<int>("number")),
(posision->second).get<string>("name")
);
but I get
no matching function for call to ‘First::First(int, std::basic_string<char>)’
so I tried casting like this:
First* one = new First(
(int&) boost::lexical_cast<int>((posision->second).get<int>("number")),
(string&) (posision->second).get<string>("name")
);
but then I got
invalid cast of an rvalue expression of type ‘int’ to type ‘int&’
and
invalid cast of an rvalue expression of type ‘std::basic_string<char>’ to type ‘std::string&
will appreciate any help or explanation.
thanks !
The issue is that you cannot pass r-values where the argument is typed as an l-value reference. E.g.
If this were valid, we would be assigning the value 2 to the value 5, which is nonsense. If it's possible, you can declare the First constructor to take const references (not sure if this works for you since you didn't post the code):
Although for primitives, it's best to just pass as value instead of const reference:
If you need them to be non-const references (which smells like a bad design), you can do: