This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Pure virtual member and namespaces.
- To: Savas dot Parastatidis at ncl dot ac dot uk
- Subject: Re: Pure virtual member and namespaces.
- From: Martin von Loewis <martin at mira dot isdn dot cs dot tu-berlin dot de>
- Date: Tue, 24 Mar 1998 08:28:28 +0100
- CC: egcs at cygnus dot com
- References: <000801bd5414$49e6e630$51d0f080@newyork.ncl.ac.uk>
> I think there is a problem while compiling the following example using the
> latest snapshot of egcs:
Dear Savas,
Thanks for your report.
> The message I get is:
> /tmp/cca11232.s: Assembler messages:
> /tmp/cca11232.s:257: Fatal error: Symbol __tfQ212FooNamespace3Foo already
> defined.
This does not happen with my current sources, so it should be fixed
when I submit them.
> #include <map>
>
> namespace X
> {
> class foo1
> {
> public:
> int bar;
> friend bool operator<(const foo1&, const foo1&);
> };
> }
>
> bool operator<(const X::foo1& rhs, const X::foo1& lhs)
> {
> cout << "using this" << endl;
> return (rhs.bar < lhs.bar);
> }
I believe the program you present is not a complete C++ program.
In particular, the operator you declare inside X::foo is in fact
X::operator<(const X::foo&, const X::foo&). [namespace.memdef] says
If a friend declaration in a nonlocal class first declares a
class or function 83) the friend class or function is a member of
the innermost enclosing namespace.
This is not a problem per se. However, you call it later. The call
will find both ::operator< and X::operator<, and determine the call to
be ambiguous. Instead, you need to use a qualifier in the friend
declaration as allowed in [decl.meaning]. This would give
namespace X
{
class foo1
{
public:
int bar;
friend bool ::operator<(const foo1&, const foo1&);
};
}
Unfortunately, scoping a friend with global scope is currently not
supported by g++. Your best bet will be define the operator inside
X.
Regards,
Martin