我还没有在共享指针上工作过。我只知道这个概念。我正在尝试调试以下c++类中的函数,该类存储XML文件的数据(通过xerces库读入)。
// header file
class ParamNode;
typedef boost::shared_ptr<ParamNode> PtrParamNode;
class ParamNode : public boost::enable_shared_from_this<ParamNode> {
public:
...
typedef enum { DEFAULT, EX, PASS, INSERT, APPEND } ActionType;
bool hasChildren() const;
PtrParamNode GetChildren();
PtrParamNode Get(const std::string& name, ActionType = DEFAULT );
protected:
....
ActionType defaultAction_;
}现在,如果我正在调试一段代码,其中有一个指向ParamNode类的指针实例,它被称为paramNode_
PtrParamNode paramNode_;
// read xml file with xerces
paramNode_ = xerces->CreateParamNodeInstance();
// now, the xml data is stored in paramNode_.
std::string probGeo;
// this works in the code, but not in (gdb)!!
paramNode_->Get("domain")->GetValue("gt",probGeo);
cout << probGeo << endl; // <-- breakpoint HERE我正在使用gdb检查paramNode_对象:
(gdb) p paramNode_
$29 = {px = 0x295df70, pn = {pi_ = 0x2957ac0}}
(gdb) p *paramNode_.px
$30 = {
<boost::enable_shared_from_this<mainclass::ParamNode>> = {weak_this_ = {px = 0x295df70, pn = {pi_ = 0x2957ac0}}},
_vptr.ParamNode = 0x20d5ad0 <vtable for mainclass::ParamNode+16>,
...
name_= {...},
children_ = {size_ = 6, capacity_ = 8, data_ = 0x2969798},
defaultAction_ = mainclass::ParamNode::EX, }并打印其成员:
(gdb) ptype *paramNode_.px
type = class mainclass::ParamNode : public boost::enable_shared_from_this<mainclass::ParamNode> {
protected:
...
mainclass::ParamNode::ActionType defaultAction_;
public:
bool HasChildren(void) const;
mainclass::PtrParamNode GetChildren(void);
mainclass::PtrParamNode Get(const std::string &, mainclass::ParamNode::ActionType);但是,我只能调用函数HasChildren或GetChildren,而从gdb调用Get会导致错误:
(gdb) p (paramNode_.px)->HasChildren()
$7 = true
(gdb) p (paramNode_.px)->GetChildren()
$8 = (mainclass::ParamNodeList &) @0x295dfb8: {
size_ = 6,
capacity_ = 8,
data_ = 0x29697a8
}
(gdb) p (paramNode_.px)->Get("domain")
Cannot resolve method mainclass::ParamNode::Get to any overloaded instance
(gdb) set overload-resolution off
(gdb) p (paramNode_.px)->Get("domain")
One of the arguments you tried to pass to Get could not be converted to what the function wants.
(gdb) p (paramNode_.px)->Get("domain", (paramNode_.px).defaultAction_)
One of the arguments you tried to pass to Get could not be converted to what the function wants.在代码中,执行Get("domain")函数可以很好地工作。为什么会这样呢?由于我对共享指针的了解有限,如果您在您的答案中包含解释,我将非常感谢。
发布于 2013-05-24 21:02:17
gdb不是一个编译器,它不会为你做(不那么好)的用户定义类型转换。如果你想调用一个需要string的函数,你需要给它一个string,而不是一个const char*。
不幸的是,gdb不能在命令行上为您构造std::string,同样是因为它不是编译器,对象创建也不是一个简单的函数调用。
所以你必须在你的程序中添加一个小的辅助函数,它将接受一个const char*并返回一个std::string&。请注意此处的引用。它不能按值返回,因为这样gdb就不能通过常量引用传递结果(它不是编译器!)您可以选择返回对静态对象的引用,也可以选择返回对堆上分配的对象的引用。在后一种情况下,它会泄漏内存,但这并不是什么大问题,因为该函数无论如何都只能从调试器中调用。
std::string& SSS (const char* s)
{
return *(new std::string(s));
}然后在gdb中
gdb> p (paramNode_.px)->Get(SSS("domain"))应该行得通。
发布于 2015-05-02 21:29:54
在这种情况下,我只是在下达命令后成功了。
set overload-resolution off发布于 2013-05-25 04:49:23
在前面的回答中增加了几个--
gdb可能最终会学会如何进行这样的转换。现在还不能;但是在改进对C++表达式解析的支持方面已经有了积极的工作。
gdb也不理解默认参数。这在一定程度上是gdb中的一个bug;但也是g++中的一个bug,它不会将它们释放到矮子中。我认为DWARF甚至没有定义一种发出重要默认参数的方法。
https://stackoverflow.com/questions/16734783
复制相似问题