在 gdb 中,我可以调用一些类函数,但其他函数"cannot be resolved" .为什么?
In gdb, I can call some class functions, but others "cannot be resolved". Why?
我还没有在共享指针上工作过。我只知道这个概念。我正在尝试调试以下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")
函数工作得很好。为什么呢?由于我对共享指针的知识有限,如果您在回答中包含解释,我将不胜感激。
gdb
不是编译器,它不会为您做(不那么)好的用户定义类型转换。如果你想调用一个需要string
的函数,你需要给它一个string
,而不是const char*
。
不幸的是,gdb
不能在命令行上为您构造std::string
,同样因为它不是编译器,对象创建也不是简单的函数调用。
因此,您必须在程序中添加一个小辅助函数,它将接受const char*
并返回std::string&
。注意这里的引用。它不能按值返回,因为这样gdb
将无法通过const引用传递结果(它不是编译器!)可以选择返回对静态对象的引用,也可以返回对在堆上分配的对象的引用。在后一种情况下,它会泄漏内存,但这不是什么大问题,因为该函数无论如何都只能从调试器调用。
std::string& SSS (const char* s)
{
return *(new std::string(s));
}
gdb
gdb> p (paramNode_.px)->Get(SSS("domain"))
。
在这种情况下,我刚刚发出命令
set overload-resolution off
对上一个答案的补充——
gdb可能最终会学习如何进行这样的转换。现在不行;但是在改进对c++表达式解析的支持方面有积极的工作。
gdb也不理解默认参数。这在一定程度上是gdb的错误;但也有一部分是g++中的错误,它不会将它们发射到DWARF中。我认为DWARF甚至没有定义一种方式来发出非平凡的默认参数。
的答案是伟大的,但为了避免不得不编辑代码和重新编译,看看在GDB中创建c++字符串给出的解决方案。
在该解决方案中,他们演示了在堆上为std::string
分配空间,然后初始化std::string
以传递给他们想从gdb
调用的function
。
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- C++ 与函数联合,Arduino编译器错误"cannot be overloaded"
- 成员函数指针"Cannot convert"问题
- 在 gdb 中,我可以调用一些类函数,但其他函数"cannot be resolved" .为什么?