By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Operator Overlading Problem

P: n/a
Hi!,

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

friend std::ostream& operator<<(std::ostream &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?
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Jacob Rohde" <zenoNOSPAM@**REMOVE*post.cybercity.dk> wrote...
I'm having problem with overloading the output operator. First I'll show you the declaration in the header:

friend std::ostream& operator<<(std::ostream &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
Jul 22 '05 #2

P: n/a

"Victor Bazarov" <v.********@comAcast.net> skrev i en meddelelse
news:L8Qzb.229623$Dw6.808771@attbi_s02...
"Jacob Rohde" <zenoNOSPAM@**REMOVE*post.cybercity.dk> wrote...
I'm having problem with overloading the output operator. First I'll show

you
the declaration in the header:

friend std::ostream& operator<<(std::ostream &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...
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.