Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 超载<&书信电报;,编译时出错_C++_Operator Overloading - Fatal编程技术网

C++ 超载<&书信电报;,编译时出错

C++ 超载<&书信电报;,编译时出错,c++,operator-overloading,C++,Operator Overloading,守则: std::ostream& operator << (std::ostream& out, point p) { out << "(" << p.x << ", "<< p.y<< ", "<< p.z << ")"; return out; } 首先,您应该通过常量引用传递点p:常量点&p,这样它就不会被重建为新对象 其次,您似乎是从错误消息中定义了一个运算符,看

守则:

std::ostream& operator << (std::ostream& out, point p) { 
  out << "(" << p.x << ", "<< p.y<< ", "<< p.z << ")";
  return out;
}

首先,您应该通过常量引用传递
点p
常量点&p
,这样它就不会被重建为新对象


其次,您似乎是从错误消息中定义了一个
运算符,看起来您是在声明您的
运算符see或作为
朋友
函数。嗯,正如所说的,此代码来自一本书,因此对于const,我同意您的看法,但我只是照着编写的方式复制。我以前没有听说过重载如果
point
很小并且有一个简单的复制构造函数,那么通过值传递它比通过const引用传递它更有效,尽管这显然取决于许多因素。而且,从它的外观来看,
KillThemAll
(!)是一个名称空间;问题是
operator@TristanBrindle,它似乎是一个3D点;初始化三个浮点变量不会比传入一个常量引用快。@CoffeeandCode是的,你说得对,我只是学究:-)事实上,既然
point
已经发布了,它实际上是三个双倍,所以常量引用会更好!好吧,你说对了,我没有注意到。。。我有
接线员
struct point{
    double x,y,z;
    point() { x=y=z=0.0;}
    point(double _x, double _y, double _z){this->x=_x, this->y=_y, this->z=_z;}
    point operator - (const point& _p) const { return point(x-_p.x, y-_p.y, z-_p.z);}
    point operator + (const point& _p) const { return point(x+_p.x, y+_p.y, z+_p.z);}
    double operator * (const point& _p) const { return x*_p.x+y*_p.y+z*_p.z;}
    point operator * (const double _t) const { return point(_t*x, _t*y, _t*z);}
    point operator / (const double _t) const { if(_t!=0) return point(x/_t, y/_t, z/_t);}
  };