namespace problems with friend function

T

Tim Partridge

I'm not very good with namespaces, so my problem is probably a simple one.
I can't get the following to compile on gcc 3.3.1. It reports

main.cc: In function 'std::eek:stream & operator<<(std::eek:stream, const
ns::C&)':
main.cc:12: error: 'int ns::C::i' is private
main.cc:17: error: within this context
main.cc: In function 'int main(int, char**)':
main.cc:23: error: ambiguous overload for 'operator<<' in 'std::cout << c'
main.cc:16: error: candidates are: std::eek:stream& operator<<(std::eek:stream&,
const ns::C&)
main.cc:9: error: std::eek:stream& ns::eek:perator<<(std::eek:stream&, const
ns::C&)
make: *** [main.o] Error 1

#include <iostream>

namespace ns {
class C {
public:
C(int i) : i(i) {};
~C() {};

friend std::eek:stream &operator<<(std::eek:stream
&stream, const C &c); // line 9
private:
int i; // line 12
};
}

std::eek:stream &operator<<(std::eek:stream &stream, const ns::C &c) {
stream << "C(" << c.i << ")" << std::endl; // line 17
return stream;
}

int main(int argc, char *argv[]) {
ns::C c(4);
std::cout << c; // line 23
return 0;
}
 
B

Brian MacBride

Tim Partridge said:
I'm not very good with namespaces, so my problem is probably a simple one.
I can't get the following to compile on gcc 3.3.1. It reports

main.cc: In function 'std::eek:stream & operator<<(std::eek:stream, const
ns::C&)':
main.cc:12: error: 'int ns::C::i' is private
main.cc:17: error: within this context
main.cc: In function 'int main(int, char**)':
main.cc:23: error: ambiguous overload for 'operator<<' in 'std::cout << c'
main.cc:16: error: candidates are: std::eek:stream& operator<<(std::eek:stream&,
const ns::C&)
main.cc:9: error: std::eek:stream& ns::eek:perator<<(std::eek:stream&, const
ns::C&)
make: *** [main.o] Error 1

#include <iostream>

namespace ns {
class C {
public:
C(int i) : i(i) {};
~C() {};

friend std::eek:stream &operator<<(std::eek:stream
&stream, const C &c); // line 9
private:
int i; // line 12
};
}

std::eek:stream &operator<<(std::eek:stream &stream, const ns::C &c) {
stream << "C(" << c.i << ")" << std::endl; // line 17
return stream;
}

int main(int argc, char *argv[]) {
ns::C c(4);
std::cout << c; // line 23
return 0;
}

Put the implementation of the friend function in the same namespace...
something like...

#include <iostream>

namespace ns {
class C {
public:
C(int i) : i(i) {}

friend std::eek:stream &operator << (std::eek:stream &, const C &);
private:
int i;
};
}

namespace ns {
std::eek:stream &operator << (std::eek:stream &o, const C &c) {
o << "C (" << c.i << ")" << std::endl;
return o;
}
}

int main (int argc, char *argv[]) {
ns::C c (4);
std::cout << c;
}

// C (4)

Regards

Brian
 
N

Nick Hounsome

Tim Partridge said:
I'm not very good with namespaces, so my problem is probably a simple one.
I can't get the following to compile on gcc 3.3.1. It reports

main.cc: In function 'std::eek:stream & operator<<(std::eek:stream, const
ns::C&)':
main.cc:12: error: 'int ns::C::i' is private
main.cc:17: error: within this context
main.cc: In function 'int main(int, char**)':
main.cc:23: error: ambiguous overload for 'operator<<' in 'std::cout << c'
main.cc:16: error: candidates are: std::eek:stream& operator<<(std::eek:stream&,
const ns::C&)
main.cc:9: error: std::eek:stream& ns::eek:perator<<(std::eek:stream&, const NB ns::eek:perator<<
ns::C&)
make: *** [main.o] Error 1

#include <iostream>

namespace ns {
class C {
public:
C(int i) : i(i) {};
~C() {};

friend std::eek:stream &operator<<(std::eek:stream
&stream, const C &c); // line 9

This says you have a friend called
ns::eek:perator<<(std::eek:stream&,const ns::C&)
private:
int i; // line 12
};
}

std::eek:stream &operator<<(std::eek:stream &stream, const ns::C &c) {
stream << "C(" << c.i << ")" << std::endl; // line 17
return stream;
}

This declares a (non-friend) global called
::eek:perator<<(std::eek:stream&,const ns::C&)
int main(int argc, char *argv[]) {
ns::C c(4);
std::cout << c; // line 23

both ::eek:perator<< and ns::eek:perator<< match hence ambiguity
return 0;
}

Personally I find it clearer never to use friends - declare a public print
method
and declare a non-friend operator<< to use it.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top