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

namespace problems with friend function

P: n/a
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::ostream & operator<<(std::ostream, 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::ostream& operator<<(std::ostream&,
const ns::C&)
main.cc:9: error: std::ostream& ns::operator<<(std::ostream&, const
ns::C&)
make: *** [main.o] Error 1

#include <iostream>

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

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

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


P: n/a

"Tim Partridge" <pr******@beta.math.uwaterloo.ca> wrote in message
news:Pi*******************************@beta.math.u waterloo.ca...
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::ostream & operator<<(std::ostream, 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::ostream& operator<<(std::ostream&,
const ns::C&)
main.cc:9: error: std::ostream& ns::operator<<(std::ostream&, const
ns::C&)
make: *** [main.o] Error 1

#include <iostream>

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

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

std::ostream &operator<<(std::ostream &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::ostream &operator << (std::ostream &, const C &);
private:
int i;
};
}

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

P: n/a

"Tim Partridge" <pr******@beta.math.uwaterloo.ca> wrote in message
news:Pi*******************************@beta.math.u waterloo.ca...
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::ostream & operator<<(std::ostream, 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::ostream& operator<<(std::ostream&,
const ns::C&)
main.cc:9: error: std::ostream& ns::operator<<(std::ostream&, const NB ns::operator<< ns::C&)
make: *** [main.o] Error 1

#include <iostream>

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

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

std::ostream &operator<<(std::ostream &stream, const ns::C &c) {
stream << "C(" << c.i << ")" << std::endl; // line 17
return stream;
}
This declares a (non-friend) global called
::operator<<(std::ostream&,const ns::C&)

int main(int argc, char *argv[]) {
ns::C c(4);
std::cout << c; // line 23
both ::operator<< and ns::operator<< 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.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.