help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Wierd friend behaviourin g++ 4.1.1


From: Paulo Matos
Subject: Wierd friend behaviourin g++ 4.1.1
Date: 15 Jun 2006 09:57:31 -0700
User-agent: G2/0.2

Hi all,

I have a piece of code which used to compile in previous g++ versions
but not on 4.1.1 and I can't find anything wrong with it:
#include <iostream>
#include <cassert>

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.
};

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]