[英]boost::property_tree put usage C++
I am doing a ptree.put()
inside a class member function. 我在类成员函数内执行
ptree.put()
。
class Writer{
private:
boost::property_tree::ptree ptree_;
public:
Writer(){}
void setData1(string path, string data){
ptree_.put(path, data);
}
void setData2(string path, string data){
ptree_.put(path, data);
}
void printPtree(){
boost::property_tree::write_json(std::cout, ptree_);
}
};
I am creating an instance of the class and setData1()
is called by a callback function in the code (Multiple ROS subscriber callbacks). 我正在创建该类的实例,并且
setData1()
由代码中的回调函数(多个ROS订户回调)调用。
When I call setData2()
in the main()
it works as expected. 当我在
main()
调用setData2()
,它可以按预期工作。 But when the similar setData1()
is called by the callback, ptree_
is empty at the end. 但是,当回调调用类似的
setData1()
, ptree_
为空。 When I do both, ptree_
only has the data written by setData2()
. 当我两者都做时,
ptree_
仅具有由setData2()
写入的数据。
When I print the ptree_
inside setData1()
at each call, data of that call can be seen in the ptree_
but not the data written in previous calls. 当我在每次调用时在
setData1()
中打印ptree_
时,可以在ptree_
看到该调用的数据,但不能看到先前调用中写入的数据。 No data written using setData1()
is there when printPtree()
is called. 没有使用写入数据
setData1()
是那里当printPtree()
被调用。 Only data written using setData1()
remains intact. 仅使用
setData1()
写入的数据保持不变。
I wrote two identical setData
methods so that I could explain my problem clearly. 我编写了两个相同的
setData
方法,以便可以清楚地说明我的问题。 What I am doing wrong here? 我在这里做错了什么?
EDIT: Here's a minimal version of my code. 编辑:这是我的代码的最低版本。 This is a ROS node.
这是一个ROS节点。
class Writer{
private:
pt::ptree ptree_;
public:
Writer(){}
void setData(string branch, string data){
ptree_.put(branch, data);
// gets ptree content at each call
// pt::write_json(std::cout, ptree_);
}
~Writer(){
pt::write_json(std::cout, ptree_);
}
};
class SubscriberHandler{
private:
ros::Subscriber sub_;
Writer writer_;
public:
SubscriberHandler(string topic_name_, Writer & writer) : writer_(writer){
ros::NodeHandle n;
sub_ = n.subscribe(topic_name_, 10, &SubscriberHandler::callback, this);
}
// this is the ROS callback function
void callback(const topic_tools::ShapeShifter::ConstPtr& msg){
--- CODE TO GET DATA FROM msg---
writer_.setData(path, value);
}
};
class SomeClass{
Writer writer;
public:
SomeClass(){
writer.setData("Velocity.x", "50");
writer.setData("Velocity.y", "10.5");
// I have to create these objects inside a class
SubscriberHandler sh("my_topic", writer);
}
};
int main(){
ros::init("node_name");
SomeClass sc;
ros::spin()
return 0;
}
This is the ptree_
I have at the end 这是我最后的
ptree_
{
"Velocity": {
"x": "50",
"y": "10.5"
}
}
but if I print the ptree_
at each call, it has the data sent at that particular call + the above. 但是,如果我在每次调用时都打印
ptree_
,则它具有在该特定调用+上面发送的数据。 Since it seems like a problem related to my C++ knowledge, I have posted the problem here instead of ROS answers. 由于这似乎是与我的C ++知识有关的问题,因此我在此处发布了该问题,而不是ROS答案。
You have two Writer
instances : 您有两个
Writer
实例:
writer
member of the SomeClass
instance created in main
. main
创建的SomeClass
实例的writer
成员。 The property tree in it has "Velocity.x"
and "Velocity.y"
set. "Velocity.x"
和"Velocity.y"
。 writer_
member of the SubscriberHandler
instance created in the SomeClass
constructor. SomeClass
构造函数中创建的SubscriberHandler
实例的writer_
成员。 This is a copy of the first Writer
instance (because the SubscriberHandler
constructor copied it). Writer
实例的副本(因为SubscriberHandler
构造函数已复制它)。 The property tree in it has "Velocity.x"
, "Velocity.y"
and path
set. "Velocity.x"
, "Velocity.y"
和path
集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.