gdb调用函数 - 如何使用std :: cout作为参数? [英] gdb call function - how to use std::cout as parameter?

查看:601
本文介绍了gdb调用函数 - 如何使用std :: cout作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  #include< iostream> 

使用namespace std;

命名空间ns
{
class D
{
int n;

public:
D(int _n):n(_n){}

void dump(ostream& os);
};
};

void
ns :: D :: dump(ostream& os)
{
os<< n =<< n<<的std :: ENDL;


int main(){

ns :: D d(200);

返回0;

在GDB中,当我发出命令时,调用d.dump(std :: cout),在行 return 0; ,我得到这个gdb错误:

 表达式中的语法错误,靠近`cout)'。 

任何建议如何在gdb调用函数中传递std :: cout?



[更新]
实际上是因为gdb,我使用7.2;在我切换到7.11.1后,它工作正常。

解决方案

我使用的是:

  GNU gdb(Ubuntu 7.11.1-0ubuntu1〜16.04)7.11.1 

我很快意识到,调试器完全没有看到 std :: cout 符号,可能是因为它没有在你的代码中使用。所以我改变了主函数,如下所示:

pre $ int main(){
std :: cout<< 你好,世界! <<的std :: ENDL;
ns :: D d(200);
返回0;
}

现在当我运行调试器时,我可以执行您的调用而不会出现问题:

 (gdb)break main 
断点1在0x400955:文件main.cpp,第24行。
(gdb)运行
启动程序:gdb_cout / main

main.cpp中的main():24
24 main(){
gdb)next
25 std :: cout<< 你好,世界! <<的std :: ENDL;
(gdb)下一个
Hello world!
26 ns :: D d(200);
(gdb)next
27 return 0;
(gdb)调用d.dump(std :: cout)
n = 200
(gdb)


I have the following code:

#include <iostream>      

using namespace std;

namespace ns
{           
    class D 
    {
        int n;

        public:
        D(int _n):n(_n){}

        void dump(ostream &os);
    };
};

void
ns::D::dump(ostream &os)
{
    os << "n=" << n << std::endl;
}

int main() {

  ns::D d(200);

  return 0;
}

In GDB, when I issue the command, call d.dump(std::cout), at the line return 0; , I get this gdb error:

A syntax error in expression, near `cout)'.

Any suggestion how I can pass "std::cout" in gdb calling function?

[UPDATE] Actually it is because of gdb, I was using 7.2; after I switch to 7.11.1, it works fine.

解决方案

I am using:

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1

I soon realized, that the debugger does not see std::cout symbol at all, probably because it is not used in your code. So I changed the main function as follows:

int main() {
  std::cout << "Hello world!" << std::endl;
  ns::D d(200);
  return 0;
}

And now when I run the debugger, I can perform your call without a problem:

(gdb) break main
Breakpoint 1 at 0x400955: file main.cpp, line 24.
(gdb) run
Starting program: gdb_cout/main

Breakpoint 1, main () at main.cpp:24
24      int main() {
(gdb) next
25        std::cout << "Hello world!" << std::endl;
(gdb) next
Hello world!
26        ns::D d(200);
(gdb) next
27        return 0;
(gdb) call d.dump(std::cout)
n=200
(gdb)

这篇关于gdb调用函数 - 如何使用std :: cout作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆