Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > namespace problems with friend function

Reply
Thread Tools

namespace problems with friend function

 
 
Tim Partridge
Guest
Posts: n/a
 
      01-07-2004
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:stream & operator<<(std: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:stream& operator<<(std:stream&,
const ns::C&)
main.cc:9: error: std:stream& ns:perator<<(std:stream&, const
ns::C&)
make: *** [main.o] Error 1

#include <iostream>

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

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

std:stream &operator<<(std: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;
}
 
Reply With Quote
 
 
 
 
Brian MacBride
Guest
Posts: n/a
 
      01-07-2004

"Tim Partridge" <> wrote in message
news 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:stream & operator<<(std: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:stream& operator<<(std:stream&,
> const ns::C&)
> main.cc:9: error: std:stream& ns:perator<<(std:stream&, const
> ns::C&)
> make: *** [main.o] Error 1
>
> #include <iostream>
>
> namespace ns {
> class C {
> public:
> C(int i) : i(i) {};
> ~C() {};
>
> friend std:stream &operator<<(std:stream
> &stream, const C &c); // line 9
> private:
> int i; // line 12
> };
> }
>
> std:stream &operator<<(std: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:stream &operator << (std:stream &, const C &);
private:
int i;
};
}

namespace ns {
std:stream &operator << (std: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


 
Reply With Quote
 
 
 
 
Nick Hounsome
Guest
Posts: n/a
 
      01-08-2004

"Tim Partridge" <> wrote in message
news 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:stream & operator<<(std: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:stream& operator<<(std:stream&,
> const ns::C&)
> main.cc:9: error: std:stream& ns:perator<<(std:stream&, const

NB ns:perator<<
> ns::C&)
> make: *** [main.o] Error 1
>
> #include <iostream>
>
> namespace ns {
> class C {
> public:
> C(int i) : i(i) {};
> ~C() {};
>
> friend std:stream &operator<<(std:stream
> &stream, const C &c); // line 9


This says you have a friend called
ns:perator<<(std:stream&,const ns::C&)

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


This declares a (non-friend) global called
:perator<<(std:stream&,const ns::C&)

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


both :perator<< and ns: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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
friend template function in distant namespace Noah Roberts C++ 2 03-22-2011 12:05 AM
friend declaration or operator overload function cross namespace. Layton C++ 2 09-27-2006 10:38 PM
Friend brings a friend. Shisha Girl MCSE 4 03-03-2006 02:42 AM
Friend brings a friend. Shisha Girl Python 0 03-02-2006 02:28 AM
making ostream& operator<< a friend function of a class in a namespace Tim Partridge C++ 1 06-02-2004 05:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57