This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Pure virtual member and namespaces.


> 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 non­local 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]