[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Wierd friend behaviourin g++ 4.1.1
From: |
Armel Asselin |
Subject: |
Re: Wierd friend behaviourin g++ 4.1.1 |
Date: |
Fri, 16 Jun 2006 14:28:15 +0200 |
> typedef int Var;
> #define var_Undef (-1)
>
> class Clause;
>
> class Lit {
> int x;
> public:
> Lit() : x(2*var_Undef) {} // (lit_Undef)
> explicit Lit(Var var, bool sign = false) : x((var+var) + (int)sign)
> { }
> friend Lit operator ~ (Lit p) { Lit q; q.x = p.x ^ 1; return q; }
>
> friend bool sign (Lit p) { return p.x & 1; }
> friend int var (Lit p) { return p.x >> 1; }
> friend int index (Lit p) { return p.x; } // A
> "toInt" method that guarantees small, positive integers suitable for
> array indexing.
> friend Lit toLit (int i) { Lit p; p.x = i; return p; } // Inverse
> of 'index()'.
> friend Lit unsign(Lit p) { Lit q; q.x = p.x & ~1; return q; }
> friend Lit id (Lit p, bool sgn) { Lit q; q.x = p.x ^ (int)sgn;
> return q; }
>
> friend bool operator == (Lit p, Lit q) { return index(p) ==
> index(q); }
> friend bool operator < (Lit p, Lit q) { return index(p) <
> index(q); } // '<' guarantees that p, ~p are adjacent in the ordering.
> };
to be honest your usage of friend seems a bit non-sense to me: friend is to
make NON MENBER methods (static or not) able to access protected and private
elements of a class. There you declare bunches of member functions as
friend: simply remove all these unnecessary 'friend' first. In fact I wonder
if you expected 'friend' to mean 'static'...
> class GClause {
> void* data;
> //GClause(void* d) {data = d;}
> GClause(void* d) : data(d) {}
> public:
>
>
> friend GClause GClause_new(Lit p) { return
> GClause((void*)((index(p) << 1) + 1)); }
> friend GClause GClause_new(Clause* c) { assert(((unsigned int)c &
> 1) == 0); return GClause((void*)c); }
>
> bool isLit () const { return ((unsigned int)data & 1) ==
> 1; }
> bool isNull () const { return data == NULL; }
> Lit lit () const { return toLit(((int)data) >> 1); }
> Clause* clause () const { return (Clause*)data; }
> bool operator == (GClause c) const { return data == c.data;
> }
> bool operator != (GClause c) const { return data != c.data;
> }
> };
>
>
> The error is:
> $ g++ -Wall --std=c++98 -c lit_friend.cc
> lit_friend.cc: In member function 'Lit GClause::lit() const':
> lit_friend.cc:39: error: 'toLit' was not declared in this scope
>
> toLit is a global friend function defined inside Lit. Can't understand
> the problem. I don't think I can define it outside lit since that would
> generate problems. Defining after Lit would make the friend declaration
> inside Lit invalid since the function would not be already defined.
> Defining it before Lit would render the argument of the function
> undefined and an incomplete declaration like: "class Lit;" would not
> work.
>
> Any ideas?
>
> Paulo Matos
>