GDB 不会执行二进制操作

gdb will not perform binary operation

本文关键字:二进制 操作 执行 GDB      更新时间:2023-10-16

>我有以下代码

#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) 

是我做错了什么还是调试器有问题?

p1+p2 等效于调用重载运算符+方法,但此运算符到方法的边界是在编译时完成的。 因此 GDB 不知道要调用哪个方法。