my_question
my_question

Reputation: 3235

gdb call function - how to use std::cout as parameter?

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.

Upvotes: 3

Views: 1747

Answers (1)

CygnusX1
CygnusX1

Reputation: 21808

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)

Upvotes: 1

Related Questions