Operator Overlading Problem

J

Jacob Rohde

Hi!,

I'm having problem with overloading the output operator. First I'll show you
the declaration in the header:

friend std::eek:stream& operator<<(std::eek:stream &os, const Vector3D
&vector);

This is declared in the public section of course.


What follows is my implementation from the .cpp file:

ostream& operator<<(ostream &os, const Vector3D &vector)
{
// Format: [x, y, z]^T
os << "[" << vector.m_x << ", " << vector.m_y << ", " << vector.m_z <<
"]^T";
return os;
}


When I compile I get

_
error C2248: 'math::Vector3D::m_x' : cannot access private member declared
in class 'math::Vector3D'
_

Which I don't understand AT ALL. The operator is declared in the class and
should as such could access private members, right?
 
V

Victor Bazarov

Jacob Rohde said:
I'm having problem with overloading the output operator. First I'll show you
the declaration in the header:

friend std::eek:stream& operator<<(std::eek:stream &os, const Vector3D
&vector);

This is declared in the public section of course.


What follows is my implementation from the .cpp file:

ostream& operator<<(ostream &os, const Vector3D &vector)
{
// Format: [x, y, z]^T
os << "[" << vector.m_x << ", " << vector.m_y << ", " << vector.m_z <<
"]^T";
return os;
}


When I compile I get

_
error C2248: 'math::Vector3D::m_x' : cannot access private member declared
in class 'math::Vector3D'
_

Which I don't understand AT ALL. The operator is declared in the class and
should as such could access private members, right?

You cannot declare operator<< a member because its first argument
(operand) is _NOT_ an object of your Vector3D class. So, please don't
tell us you managed to do so, because it's impossible.

What you have done is to declare operator<< a _friend_, but apparently
your compiler doesn't think so, since it doesn't let you use private
members in the function.

Try this:

class Vector3D {
...
ostream& print(ostream& os) const {
// place all output here
return os;
}

// no friends needed!!!
};

// now outside you have the output operator
ostream& operator<<(ostream& os, math::Vector3D const& v) {
return v.print(os);
}

Victor
 
J

Jacob Rohde

Victor Bazarov said:
Jacob Rohde said:
I'm having problem with overloading the output operator. First I'll show you
the declaration in the header:

friend std::eek:stream& operator<<(std::eek:stream &os, const Vector3D
&vector);

This is declared in the public section of course.


What follows is my implementation from the .cpp file:

ostream& operator<<(ostream &os, const Vector3D &vector)
{
// Format: [x, y, z]^T
os << "[" << vector.m_x << ", " << vector.m_y << ", " << vector.m_z <<
"]^T";
return os;
}


When I compile I get

_
error C2248: 'math::Vector3D::m_x' : cannot access private member declared
in class 'math::Vector3D'
_

Which I don't understand AT ALL. The operator is declared in the class and
should as such could access private members, right?

You cannot declare operator<< a member because its first argument
(operand) is _NOT_ an object of your Vector3D class. So, please don't
tell us you managed to do so, because it's impossible.

What you have done is to declare operator<< a _friend_, but apparently
your compiler doesn't think so, since it doesn't let you use private

It's not the compiler, it's me (doh :)), I forgot the namespace
qualifier...now it works!

Thanks for the response...
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top