C++ gdb将不执行二进制操作

C++ gdb将不执行二进制操作,c++,gdb,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) {

我有以下代码

#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;
}
#包括
使用名称空间std;
类Point2D
{
公众:
双x;
双y;
点2D(双x_i,
双y_i):
x(x_i),y(y_i){}
};
点2D运算符+(常量点2D和p1,
常量点(2D和p2)
{
返回点2d(p1.x+p2.x,p1.y+p2.y);
}
点2D运算符*(双s、常量点2D和p)
{
返回点2d(p.x*s,p.y*s);
}

ostream&operatorp1+p2相当于调用重载运算符+方法,但此运算符到方法的绑定在编译时完成。因此,gdb不知道调用哪个方法。

与(+1)问题无关,但
运算符*
运算符+
应该是
const
。“反向”运算符(点*double)工作(稍微-
p1*0.5
正常),但不是double*点(
0.5*p1
不正常)。如果这是gdb表达式计算器的一个限制,我不会感到惊讶,但我不确定。令人烦恼的是,
operator*(0.5,p1)
可以工作,但是
operator*(0.5,p1+p2)
(p1+p2)*0.5都失败了。(“尝试获取不在内存中的值的地址”)。我会本能地责怪gdb,而不是你的代码。
$ 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)