POV-Ray : Newsgroups : povray.off-topic : C++ question : Re: C++ question Server Time
28 Jul 2024 16:29:27 EDT (-0400)
  Re: C++ question  
From: Le Forgeron
Date: 3 Oct 2013 15:20:15
Message: <524dc36f@news.povray.org>
Le 03/10/2013 20:47, Anthony D. Baye nous fit lire :
> I forgot to uncomment the function definition in point3d.cpp before I
> uploaded...
> 
> and I made a stupid in my comment:  I forgot that friend functions can access
> protected members, but not private members, so you were right that it would be
> better off as a free function within the namespace, but it still doesn't work
> right when I compile uTest.

the operator<< must be implemented in geometry namespace, and friend is
not enough to declare a function.

Enjoy.

diff -r 9b234d20a210 -r 1840dc7961c9 geometry.h
--- a/geometry.h        Thu Oct 03 21:18:39 2013 +0200
+++ b/geometry.h        Thu Oct 03 21:17:32 2013 +0200
@@ -28,6 +28,7 @@
     private:
         double coords[3];
     };
+               ostream &operator <<(ostream &, const point3d &);

     // 3D geometric vector class extends point3d through inheritance.
     // Angles are measured in radians.
diff -r 9b234d20a210 -r 1840dc7961c9 point3d.cpp
--- a/point3d.cpp       Thu Oct 03 21:18:39 2013 +0200
+++ b/point3d.cpp       Thu Oct 03 21:17:32 2013 +0200
@@ -62,9 +62,9 @@
 //    return os;
 //}

-//ostream &operator <<(ostream &os, const point3d &P)
-//{
-//    os << "(" << P.coords[X] << ", " << P.coords[Y] << ", " <<
P.coords[Z] << ")" << endl;
+ostream & geometry::operator <<(ostream &os, const point3d &P)
+{
+    os << "(" << P.coords[X] << ", " << P.coords[Y] << ", " <<
P.coords[Z] << ")" << endl;

-//    return os;
-//}
+    return os;
+}


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.