I'm trying to build a program whose source I downloaded from the internet. When I try to compile it, I get the error message
friend declaration specifying a default argument must be the only declaration
Here is the offending code:
typedef int Var;
struct Lit {
int x;
// Use this as a constructor:
friend Lit mkLit(Var var, bool sign = false);
bool operator == (Lit p) const { return x == p.x; }
bool operator != (Lit p) const { return x != p.x; }
bool operator < (Lit p) const { return x < p.x; }
inline Lit mkLit(Var var, bool sign) { Lit p; p.x = var + var + (int)sign; return p; }
I've found several questions that address this issue, but the matter is rather abstruse, and I don't really understand the explanations. The answer to this question indicates that I can fix matters by moving the inline defintion of mkLit
before the struct, removing the default argument from the friend function declaration, and moving it to the inline definition. Is this correct?
But more basically, I don't understand why a struct needs a friend function, since its members are public anyway. The accepted answer to this question gives an answer (that my ignorance prevents me from understanding) in terms of argument dependent lookup, but I can't see that it applies in this case.
Is there any drawback to just deleting the friend function declaration, and moving the default argument to the inline function definition? If so, can you give me a simple example?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…