Question on operator << overloading

S

shuisheng

Dear All,

I have a question on operator << overloading on a class in a
namespace. For example:

#include <iostream>

namespace Geometry
{
class Point
{
friend std::eek:stream& operator << (std::eek:stream& out, const Point&
pt);

private:
double x, y, z;
};
};

inline std::eek:stream& operator << (std::eek:stream& out, const Point& pt)
{
out << pt.x << pt.y << pt.z;
return out
}

I got compilation errors: can not access class Point 's private
members x, y, and z. If I move the overloading function into the
Geometry namespace, it is fine. But I want to keep the function
global and do not want to make x, y, z public, how can I do for it?

Thank you for your kind help in advance!

Shuisheng
 
R

red floyd

Dear All,

I have a question on operator << overloading on a class in a
namespace. For example:

#include <iostream>

namespace Geometry
{
  class Point
  {
    friend std::eek:stream& operator << (std::eek:stream& out, const Point&
pt);

  private:
    double x, y, z;
  };

};

inline std::eek:stream& operator << (std::eek:stream& out, const Point& pt)
{
  out << pt.x << pt.y << pt.z;
  return out

}

I got compilation errors: can not access class Point 's private
members x, y, and z. If I move the overloading function into the
Geometry namespace, it is fine. But I want to keep  the function
global and do not want to make x, y, z public, how can I do for it?

Thank you for your kind help in advance!

The problem is that you've specified operator<< in Geometry as the
friend, not the operator<<
in the global namespace.


I'm not sure how to use the scope resolution operator on an operator
(I tried ::eek:perator<< in the friend decl and got errors.).

But, as has been said, there's nothing in C++ that you can't cure with
an extra level of indirection.

#include <iostream>
namespace Geometry
{
class Point
{
friend std::eek:stream& Print(std::eek:stream& out, const Point& pt);

private:
double x, y, z;
};


inline std::eek:stream& operator << (std::eek:stream& out, const Point&
pt)
{
return Print(out,pt);
}

inline std::eek:stream& Print(std::eek:stream& out, const Point& pt)
{
out << pt.x << pt.y << pt.z;
return out;
}

}

inline std::eek:stream& operator<<(std::eek:stream& out, const
Geometry::point& pt)
{
return Geometry::print(out, pt);
}
 
A

Alf P. Steinbach

* shuisheng:
I have a question on operator << overloading on a class in a
namespace. For example:

#include <iostream>

namespace Geometry
{
class Point
{
friend std::eek:stream& operator << (std::eek:stream& out, const Point&
pt);

private:
double x, y, z;
};
};

inline std::eek:stream& operator << (std::eek:stream& out, const Point& pt)
{
out << pt.x << pt.y << pt.z;
return out
}

I got compilation errors: can not access class Point 's private
members x, y, and z. If I move the overloading function into the
Geometry namespace, it is fine. But I want to keep the function
global and do not want to make x, y, z public, how can I do for it?

<code file="A.cpp">
#include <iostream>

namespace Geometry {
class Point;
}

inline std::eek:stream& operator<<(
std::eek:stream& out,
Geometry::point const& pt
);


namespace Geometry {
class Point
{
friend std::eek:stream& ::eek:perator<<(
std::eek:stream& out,
Geometry::point const& pt
);

private:
double x, y, z;
};
}

inline std::eek:stream& operator<<(
std::eek:stream& out,
Geometry::point const& pt
)
{
return out << pt.x << " " << pt.y << " " << pt.z;
}
</code>


<code file="B.cpp">
#include <iostream>

namespace Geometry {
class Point
{
public:
std::eek:stream& writeOn( std::eek:stream& out ) const
{
return out << x << " " << y << " " << z;
}

private:
double x, y, z;
};
}

inline std::eek:stream& operator<<(
std::eek:stream& out,
Geometry::point const& pt
)
{
return pt.writeOn( out );
}
</code>


Cheers & hth.,

- Alf
 
D

Daniel Vogelbacher

shuisheng said:
Dear All,

I have a question on operator << overloading on a class in a
namespace. For example:

#include <iostream>

namespace Geometry
{
class Point
{
friend std::eek:stream& operator << (std::eek:stream& out, const Point&
pt);

private:
double x, y, z;
};
};

inline std::eek:stream& operator << (std::eek:stream& out, const Point& pt)
{
out << pt.x << pt.y << pt.z;
return out
}

I got compilation errors: can not access class Point 's private
members x, y, and z. If I move the overloading function into the
Geometry namespace, it is fine. But I want to keep the function
global and do not want to make x, y, z public, how can I do for it?

Thank you for your kind help in advance!

Shuisheng

You should declare the overloaded operator<< in the global scope:

///////////////////////////////////////////////////////////////////////////////
#include <iostream>

namespace Geometry
{
class Point;
}

// Important!.
// Declaration in global namespace
std::eek:stream& operator << (std::eek:stream& out,
const Geometry::point& pt);


namespace Geometry
{
class Point
{
public:
// friend declaration.
// Now we can access the private members.
friend std::eek:stream& ::eek:perator << (std::eek:stream& out,
const Geometry::point& pt);

Point(void) : x(0), y(1), z(2)
{ }

private:
double x, y, z;
};

}

//implementation
inline std::eek:stream& operator << (std::eek:stream& out, const Geometry::point& pt)
{
out << pt.x << pt.y << pt.z;
return out;
}

int main(void)
{
using Geometry::point;
Point x;
std::cout << x << std::endl;
return 0;
}
///////////////////////////////////////////////////////////////////////////////

Compiles fine with gcc -Wall -pedantic -std=c++98.
I hope this helps.

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top