T
Tony Johansson
Hello Experts!
I have the user defined class called Boolean that should be handle all kind
of logic expression such
as if (a && b && c) or if (!a && b && c) osv
I have the class definition Boolean and the main program below.
I have overloaded these operator symbols =, ==, !=, !, ||, &&.
Now to my question I have some problem with this Class Boolean that I use as
a trush table.
First this kind of expression if (!b && c) {} gives this compile error
"c:\Documents and Settings\Tony\kau\cplusplus\test10\start.cpp(23): error
C2677: binary '&&' : no global operator found which takes type 'Boolean' (or
there is no acceptable conversion)"
This statement if (b && !c) {} cause this compile error
"c:\Documents and Settings\Tony\kau\cplusplus\test10\start.cpp(26): error
C2679: binary '&&' : no operator found which takes a right-hand operand of
type 'bool' (or there is no acceptable conversion)"
As you can see the problem depends on that !b doesnt return an object of
class Boolean but a type of bool.
This expression cause .if (b && c && a) this compile error
"c:\Documents and Settings\Tony\kau\cplusplus\test10\start.cpp(29): error
C2677: binary '&&' : no global operator found which takes type 'Boolean' (or
there is no acceptable conversion)"
So what should I return from these overloaded operators.
class Boolean
{
public:
enum TruthValue{FALSE, TRUE};
Boolean(TruthValue tv)
{
truth_value = new TruthValue;
*truth_value = *(src.truth_value);
}
Boolean(const Boolean& src)
{
truth_value = new TruthValue;
*truth_value = *(src.truth_value);
}
virtual ~Boolean()
{
delete truth_value;
truth_value = 0;
}
TruthValue getValue()
{ return *truth_value; }
Boolean& operator=(const Boolean&);
bool operator==(const Boolean&);
bool operator!=(const Boolean&);
bool operator!();
bool operator&&(const Boolean&);
bool operator||(const Boolean&);
private:
TruthValue* truth_value;
};
bool Boolean:perator||(const Boolean& h)
{ return *truth_value || *(h.truth_value); }
bool Boolean:perator&&(const Boolean& h)
{ return *truth_value && *(h.truth_value); }
bool Boolean:perator!()
{ return !(*truth_value); }
bool Boolean:perator==(const Boolean& h)
{ return *truth_value == *(h.truth_value); }
bool Boolean:perator!=(const Boolean& h)
{ return !(*this == h); }
Boolean& Boolean:perator=(const Boolean& h)
{
if (this != &h)
{
delete truth_value;
truth_value = new TruthValue;
*truth_value = *(h.truth_value);
}
return *this;
}
#include <iostream>
#include "boolean.h"
using namespace std;
int main()
{
Boolean a(Boolean::TRUE);
Boolean b(Boolean::FALSE);
a=b;
a = Boolean::TRUE;
Boolean c = a;
if (a!= b) cout << "correct";
if (!b) cout << "not correct";
if (b && c) cout << "correct";
if (!b && c) cout << "correct";
if (b && !c) cout << "not correct";
if (b && c && a) cout << "not correct";
return 0;
}
Many thanks
//Tony
I have the user defined class called Boolean that should be handle all kind
of logic expression such
as if (a && b && c) or if (!a && b && c) osv
I have the class definition Boolean and the main program below.
I have overloaded these operator symbols =, ==, !=, !, ||, &&.
Now to my question I have some problem with this Class Boolean that I use as
a trush table.
First this kind of expression if (!b && c) {} gives this compile error
"c:\Documents and Settings\Tony\kau\cplusplus\test10\start.cpp(23): error
C2677: binary '&&' : no global operator found which takes type 'Boolean' (or
there is no acceptable conversion)"
This statement if (b && !c) {} cause this compile error
"c:\Documents and Settings\Tony\kau\cplusplus\test10\start.cpp(26): error
C2679: binary '&&' : no operator found which takes a right-hand operand of
type 'bool' (or there is no acceptable conversion)"
As you can see the problem depends on that !b doesnt return an object of
class Boolean but a type of bool.
This expression cause .if (b && c && a) this compile error
"c:\Documents and Settings\Tony\kau\cplusplus\test10\start.cpp(29): error
C2677: binary '&&' : no global operator found which takes type 'Boolean' (or
there is no acceptable conversion)"
So what should I return from these overloaded operators.
class Boolean
{
public:
enum TruthValue{FALSE, TRUE};
Boolean(TruthValue tv)
{
truth_value = new TruthValue;
*truth_value = *(src.truth_value);
}
Boolean(const Boolean& src)
{
truth_value = new TruthValue;
*truth_value = *(src.truth_value);
}
virtual ~Boolean()
{
delete truth_value;
truth_value = 0;
}
TruthValue getValue()
{ return *truth_value; }
Boolean& operator=(const Boolean&);
bool operator==(const Boolean&);
bool operator!=(const Boolean&);
bool operator!();
bool operator&&(const Boolean&);
bool operator||(const Boolean&);
private:
TruthValue* truth_value;
};
bool Boolean:perator||(const Boolean& h)
{ return *truth_value || *(h.truth_value); }
bool Boolean:perator&&(const Boolean& h)
{ return *truth_value && *(h.truth_value); }
bool Boolean:perator!()
{ return !(*truth_value); }
bool Boolean:perator==(const Boolean& h)
{ return *truth_value == *(h.truth_value); }
bool Boolean:perator!=(const Boolean& h)
{ return !(*this == h); }
Boolean& Boolean:perator=(const Boolean& h)
{
if (this != &h)
{
delete truth_value;
truth_value = new TruthValue;
*truth_value = *(h.truth_value);
}
return *this;
}
#include <iostream>
#include "boolean.h"
using namespace std;
int main()
{
Boolean a(Boolean::TRUE);
Boolean b(Boolean::FALSE);
a=b;
a = Boolean::TRUE;
Boolean c = a;
if (a!= b) cout << "correct";
if (!b) cout << "not correct";
if (b && c) cout << "correct";
if (!b && c) cout << "correct";
if (b && !c) cout << "not correct";
if (b && c && a) cout << "not correct";
return 0;
}
Many thanks
//Tony