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

Question on friend functions and accessing private members

P: n/a
Why does my compiler complain with the message "`int*My::Test::_shape'
is private" when I try to compile the following code? It seems it has
something to do with the namespace since when I leave that out then
everything works fine.

Many thanks for your help.
Hans

// START CODE
#include <iostream>

namespace My
{ // Begin of namespace 'My'

class Test
{
private:
int* _shape;
public:

friend std::ostream& operator<<(std::ostream&, Test&);
};

} // End of namespace 'My'

std::ostream& operator<<(std::ostream& s, My::Test& t)
{
t._shape = 0;
return s;
}

int main(int argc, const char* argv[])
{
return 0;
}

// END CODE
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Hans De Winter" <ha**********@tiscalinet.be> wrote in message
news:87**************************@posting.google.c om...
Why does my compiler complain with the message "`int*My::Test::_shape' is private" when I try to compile the following code? It seems it has something to do with the namespace since when I leave that out then
everything works fine.

Many thanks for your help.
Hans

// START CODE
#include <iostream>

namespace My
{ // Begin of namespace 'My'

class Test
{
private:
int* _shape;
public:

friend std::ostream& operator<<(std::ostream&, Test&);
};

} // End of namespace 'My' std::ostream& operator<<(std::ostream& s, My::Test& t)
{
t._shape = 0;
return s;
}


The problem is the operator<< you've decalred to be a friend is in the
namespace My.

Try:

namespace My { class Test; }
friend std::ostream& operator<<(std::ostream&, My::Test&);

class Test {
friend std::ostream& ::operator<<(std::ostream&, Test&);
// ...
};

HTH

Jonathan
Jul 22 '05 #2

P: n/a
Ian
Jonathan Turkanis wrote:
"Hans De Winter" <ha**********@tiscalinet.be> wrote in message
news:87**************************@posting.google.c om...
Why does my compiler complain with the message


"`int*My::Test::_shape'
is private" when I try to compile the following code? It seems it


has
something to do with the namespace since when I leave that out then
everything works fine.

Many thanks for your help.
Hans

// START CODE
#include <iostream>

namespace My
{ // Begin of namespace 'My'

class Test
{
private:
int* _shape;
public:

friend std::ostream& operator<<(std::ostream&, Test&);
};

} // End of namespace 'My'


std::ostream& operator<<(std::ostream& s, My::Test& t)
{
t._shape = 0;
return s;
}

The problem is the operator<< you've decalred to be a friend is in the
namespace My.

Try:

namespace My { class Test; }
friend std::ostream& operator<<(std::ostream&, My::Test&);

class Test {
friend std::ostream& ::operator<<(std::ostream&, Test&);
// ...
};

Or just
std::ostream& My::operator<<(std::ostream& s, My::Test& t)
{
t._shape = 0;
return s;
}

Ian

Jul 22 '05 #3

P: n/a

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:bu************@ID-216073.news.uni-berlin.de...
"Hans De Winter" <ha**********@tiscalinet.be> wrote in message
news:87**************************@posting.google.c om...
Why does my compiler complain with the message

"`int*My::Test::_shape'
is private" when I try to compile the following code? It seems it

has
something to do with the namespace since when I leave that out then
everything works fine.

Many thanks for your help.
Hans

// START CODE
#include <iostream>

namespace My
{ // Begin of namespace 'My'

class Test
{
private:
int* _shape;
public:

friend std::ostream& operator<<(std::ostream&, Test&);
};

} // End of namespace 'My'

std::ostream& operator<<(std::ostream& s, My::Test& t)
{
t._shape = 0;
return s;
}


The problem is the operator<< you've decalred to be a friend is in the
namespace My.

Try:

namespace My { class Test; }
friend std::ostream& operator<<(std::ostream&, My::Test&);

class Test {
friend std::ostream& ::operator<<(std::ostream&, Test&);
// ...
};

HTH

Jonathan


Or you could define << in the namespace itself.

Something like:

#include <iostream>

namespace My {

class Test {
private:
int shape;
public:
Test():shape(0){}
Test(int i):shape(i){}

friend std::ostream& operator<<(std::ostream&, Test&);
};
std::ostream& operator<<(std::ostream& s, Test& t)
{
s << t.shape;
return s;
}
}

int main(int argc, const char* argv[])
{
My::Test t(100);
std::cout << t << '\n';
}


Regards,
Sumit.
Jul 22 '05 #4

P: n/a

"Sumit Rajan" <su********@myrealbox.com> wrote in message
news:bu************@ID-206022.news.uni-berlin.de...

Or you could define << in the namespace itself.


Or you could separate the implementation from the interface by declaring <<
withing the within the namespace and defining it at a different location.

#include <iostream>

namespace My {

class Test {
private:
int shape;
public:
Test():shape(0){}
Test(int i):shape(i){}

friend std::ostream& operator<<(std::ostream&, Test&);
};
std::ostream& operator<<(std::ostream& s, Test& t);

}
std::ostream& My::operator<<(std::ostream& s, My::Test& t)
{
s << t.shape;
return s;
}

int main(int argc, const char* argv[])
{
My::Test t(100);
std::cout << t << '\n';
}

Regards,
Sumit.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.