gdb不会执行二进制操作

时间:2014-11-17 10:30:55

标签: c++ gdb

我有以下代码

#include <iostream>

using namespace std;

class Point2D
{
public:

  double x;
  double y;

  Point2D(double x_i,
      double y_i):
    x(x_i), y(y_i) {}
};

Point2D operator+(const Point2D& p1,
          const Point2D& p2)
{
  return Point2D(p1.x+p2.x, p1.y+p2.y);
}

Point2D operator*(double s, const Point2D& p)
{
  return Point2D(p.x*s,p.y*s);
}

ostream& operator<<(ostream& os, const Point2D& p)
{
  os << p.x << " " << p.y << endl;
  return os;
}

int main(void)
{
  const Point2D p1(2,3);
  const Point2D p2(5,4);

  cout << 0.5*(p1+p2) << endl;

  return 0;
}

我可以编译它并获得正确的结果,但是当我尝试调试它时遇到了麻烦。显然,编译器可以弄清楚如何进行二进制操作,但调试器却不能。我的编译器版本是g ++(Ubuntu 4.9.1-16ubuntu6)4.9.1。

$ g++ -g ./for_stack_overflow.cpp && gdb ./a.out 
GNU gdb (Ubuntu 7.8-1ubuntu4) 7.8.0.20141001-cvs
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
(gdb) break 41
Breakpoint 1 at 0x400b90: file ./for_stack_overflow.cpp, line 41.
(gdb) r
Starting program: /home/almog/Documents/a.out 
3.5 3.5


Breakpoint 1, main () at ./for_stack_overflow.cpp:41
41    return 0;
(gdb) print 0.5*(p1+p2)
Can't do that binary op on that type
(gdb) 

我做错了什么或者调试器有问题吗?

1 个答案:

答案 0 :(得分:0)

p1 + p2相当于调用重载的operator +方法,但是此方法绑定的运算符是在编译时完成的。因此gdb不知道要调用哪种方法。