Question on friend functions and accessing private members

H

Hans De Winter

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::eek:stream& operator<<(std::eek:stream&, Test&);
};

} // End of namespace 'My'



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



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

// END CODE
 
J

Jonathan Turkanis

Hans De Winter said:
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::eek:stream& operator<<(std::eek:stream&, Test&);
};

} // End of namespace 'My'
std::eek:stream& operator<<(std::eek:stream& 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::eek:stream& operator<<(std::eek:stream&, My::Test&);

class Test {
friend std::eek:stream& ::eek:perator<<(std::eek:stream&, Test&);
// ...
};

HTH

Jonathan
 
I

Ian

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

Try:

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

class Test {
friend std::eek:stream& ::eek:perator<<(std::eek:stream&, Test&);
// ...
};
Or just


std::eek:stream& My::eek:perator<<(std::eek:stream& s, My::Test& t)
{
t._shape = 0;
return s;
}

Ian
 
S

Sumit Rajan

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

Try:

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

class Test {
friend std::eek:stream& ::eek:perator<<(std::eek:stream&, 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::eek:stream& operator<<(std::eek:stream&, Test&);
};


std::eek:stream& operator<<(std::eek:stream& 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.
 
S

Sumit Rajan

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::eek:stream& operator<<(std::eek:stream&, Test&);
};


std::eek:stream& operator<<(std::eek:stream& s, Test& t);

}


std::eek:stream& My::eek:perator<<(std::eek:stream& 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.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top