473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

problem with a truth table

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::operator||(const Boolean& h)
{ return *truth_value || *(h.truth_value); }

bool Boolean::operator&&(const Boolean& h)
{ return *truth_value && *(h.truth_value); }

bool Boolean::operator!()
{ return !(*truth_value); }

bool Boolean::operator==(const Boolean& h)
{ return *truth_value == *(h.truth_value); }

bool Boolean::operator!=(const Boolean& h)
{ return !(*this == h); }

Boolean& Boolean::operator=(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
Aug 18 '05 #1
6 2170
Tony Johansson wrote:
bool Boolean::operator||(const Boolean& h)
{ return *truth_value || *(h.truth_value); }

bool Boolean::operator&&(const Boolean& h)
{ return *truth_value && *(h.truth_value); }


Binary operators should be implemented as non-member
functions, to solicit implicit conversions on the
left-hand argument. It looks as if that's (at least part
of) your problem.

Marc
Aug 18 '05 #2
Marc Mutz wrote:
bool Boolean::operator||(const Boolean& h)
{ return *truth_value || *(h.truth_value); }

bool Boolean::operator&&(const Boolean& h)
{ return *truth_value && *(h.truth_value); }


Binary operators should be implemented as non-member
functions, to solicit implicit conversions on the
left-hand argument. It looks as if that's (at least part
of) your problem.


Sorry. The problem is that you return bool, but don't have
an implicit conversion from bool to Boolean defined. So
the first && takes the two Boolean, return a bool and
then the second && has a bool as the left-hand argument,
and a Boolean on the right-hand side. Since there's
neither a Boolean->bool nor a bool->Boolean conversion
defined in Boolean, the call can't be satified.

Fix: return Boolean instead of bool from operator&& and
||.

Marc
Aug 18 '05 #3
Tony Johansson wrote:
[lots of snips]
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 think if you are going to create a Boolean class, then you
have to not use such things as

if (!b) cout << "not correct";

that is, you have to include the explicit full test.

if ((!b) == Boolean(Boolean::TRUE) ) cout << "not correct";

Or whatever. You want to have !b mean the Boolean
that has the opposite truth value, it has to return
a Boolean. Similarly, you will need to have

if ((a && b && c) == Boolean(Boolean::TRUE))

and so on. And == returns true/false.
Socks

Aug 18 '05 #4

"Marc Mutz" <ma**@klaralvdalens-datakonsult.se> skrev i meddelandet
news:de*************@news.t-online.com...
Marc Mutz wrote:
bool Boolean::operator||(const Boolean& h)
{ return *truth_value || *(h.truth_value); }

bool Boolean::operator&&(const Boolean& h)
{ return *truth_value && *(h.truth_value); }


Binary operators should be implemented as non-member
functions, to solicit implicit conversions on the
left-hand argument. It looks as if that's (at least part
of) your problem.


Sorry. The problem is that you return bool, but don't have
an implicit conversion from bool to Boolean defined. So
the first && takes the two Boolean, return a bool and
then the second && has a bool as the left-hand argument,
and a Boolean on the right-hand side. Since there's
neither a Boolean->bool nor a bool->Boolean conversion
defined in Boolean, the call can't be satified.

Fix: return Boolean instead of bool from operator&& and
||.

Marc


I can return a Boolean from operator&& and operator||
but have you any idea how the definition should look like.

Here it's current definition

bool Boolean::operator&&(const Boolean& h)
{ return *truth_value && *(h.truth_value); }

//Tony
Aug 18 '05 #5
Tony Johansson wrote:

I can return a Boolean from operator&& and operator||
but have you any idea how the definition should look
like.

Here it's current definition

bool Boolean::operator&&(const Boolean& h)
{ return *truth_value && *(h.truth_value); }


Boolean operator&&( const Boolean & lhs, const Boolean &
rhs ) {
return Boolean( calculate-result-here );
}

where calculate-result-here could be a table lookup or a
nested ternary operator (?:) invocation. It's your class,
after all. One example could
return Boolean( *lhs.truth_value && *rhs.truth_value
? Boolean::TRUE : Boolean::FALSE );
which relies on the implicit conversion from enum to bool,
like your original code.

Marc

Aug 18 '05 #6
Wait ONE!!!
Check Scott Meyer's "Effective C++/
More Effective C++" on why
overloading || and && is a BAD
idea. To summarize, since overloading is a function call, and
BOTH args are evaluated, you
CANNOT duplicate the short-circuit
behaviour of these operators.
This will PROBABLY lead to very
subtle and hard-to-find bugs.

Of course, if this is just an exercise --
NEVERMIND.

Ihor Kinal.

Aug 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mario | last post by:
Can some1 help me with the code for a truth table????
1
by: Snick | last post by:
Hello, I've been going through the DB2 application development manuals and have no idea what I'm doing wrong with my UDF. Basically, I created a C++ library that compares 2 strings and returns...
2
by: rossum | last post by:
I am using Visual C# Express Beta, though I suspect that my question has more to do with .NET than with C# specifically. I have written a form with a Web Browser in it. At the top I have a button...
15
by: Együd Csaba | last post by:
Hi All, I've a problem with the perfprmance of the production environment. I've two db servers. One on my laptop computer (2Ghz, 1GB, WinXP, Cygwin, Postgres 7.3.4) and one on a production server...
1
by: Rohanrajs82 | last post by:
Hi, Can anybody help me? I have to construct a truth table of 97 columns. I have tester channels from 1 to 97. I have to make a truth table with all the possible combinations. Also, I have some...
6
by: ambanks04 | last post by:
ok taking computer science class anybody know what to do i am not computer literate CoSc 111.003 Spring 2008 Assignment 4 1) Each student shall design, develop, code, test and submit an...
5
by: smittie31 | last post by:
I am having a problem with a border around me html page. The border does not flow thru the whole html page, it cuts off halfway. --> See http://keithborom.com/marlon-sanders CSS STYLESHEET ...
18
by: LosLobo | last post by:
Greetings all. I know that cascading lists are a common problem and in truth I my initial post here was to request help with my own, but then I figured out the right code. That being said, I have a...
5
by: andrea | last post by:
Well I would like to make a little program that given a certain logical expression gives the complete truth table. It's not too difficult in fact, I just have some doubts on how to design it. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.