-2

the code :

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

the error:

‘std::ostream& KillThemAll::point::operator<<(std::ostream&, KillThemAll::point)’ must take exactly one argument std::ostream& operator<< (std::ostream& out, point p) { out << "(" << p.x << ", "<< p.y<< ", "<< p.z << ")"; return out;}

actualy, the code is similar at the one p.402 of « Problems & Solutions In Scientific Computing » (except I'm using 3D instead of 2D).

for your curiosity, here the mighty structure « 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);}
  };
S..
  • 5,511
  • 2
  • 36
  • 43
The Unholy Metal Machine
  • 1,093
  • 2
  • 17
  • 36

2 Answers2

1

From the error message, it looks like you're declaring your operator<< as a member function in the point class. This is not what you want.

Instead, declare it as a free function (outside of your point class). If it needs to access private members of point, then make it a friend.

Why?

When you think about it, it's obvious that any operator<< must have exactly two arguments; one to the left of the <<, and one to the right. If you declare the operator in-class, then the left argument is automatically taken to be the object in question (this), so you can only tell it what type the right argument should have. If you declare it as a free function, then you get to choose both the left and right argument types.

Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
  • ok you got the point, I didn't take care enough... I have `operator<<` in `point` (wich is a `struct`)... it's a mistake from me, I agree it shouldn't be there! so thank you! – The Unholy Metal Machine Oct 31 '13 at 03:34
0

First, you should pass point p by const reference: const point& p so it isn't getting reconstructed as a new object.

secondly, you seem to be defining a operator<< function within class KillThemAll this is not legal overloading for stream output/input; you must define it as a free and, if it needs to access private members, friend function.

RamblingMad
  • 5,332
  • 2
  • 24
  • 48
  • 2
    Or as a `friend` function. – David G Oct 31 '13 at 03:23
  • well, as said this code comes from a book, so for the const, I'm agree with you but I just copied like it's written. I didn't heard before today that overloading << must be a free function, in the book it is, I'm using a namespace for personnal convenience. Thank you – The Unholy Metal Machine Oct 31 '13 at 03:24
  • If `point` is small and has a trivial copy constructor, it can be more efficient to pass it by value than by const reference, although obviously this depends on many factors. Also, from the look of it `KillThemAll` (!) is a namespace; the problem is that the `operator<<` is being declared in the `point` class. – Tristan Brindle Oct 31 '13 at 03:27
  • @TristanBrindle, It seems to be a 3D point; initializing three float variables wouldn't be faster than passing in a const reference. – RamblingMad Oct 31 '13 at 03:40
  • @CoffeeandCode yeah, you're right, I was just being pedantic :-) Actually, now that `point` has been posted it's actually three doubles, so a const reference would be much better! – Tristan Brindle Oct 31 '13 at 03:55