1

I'm writing a code for finding truth table but there's an ambiguous error for conjunction(bool, bool), but I don't understand how I can make it right. Does anyone have any tips or comments about this? My code:

bool conjunction(bool, bool);
bool disjunction(bool, bool);
    
int main()
{
    bool p, q, A, B, C, D;
    for (int a = 0; a < 1; a++) {
        A = a;
        for (int b = 0; b < 1; b++) {
            B = b;
            for (int c = 0; c < 1; c++) {
                C = c;
                for (int d = 0; d < 1; d++) {
                    D = d;
                    cout << A << "|" << B << "|" << C << "|" << D << endl;
                    cout << conjunction(!A, B) << endl;
                }
            }
        }
    }
}
bool conjunction(bool p, bool q)
{
    return p && q;
}
bool disjunction(bool p, bool q)
{
    return p || q;
}
  • 2
    what exactly does your compiler say about that? – Stefan Riedel Nov 08 '21 at 15:51
  • I don't see any error when compiling and running your code (https://ideone.com/MvmhiI). Please include the full error you're seeing along with where you're seeing it. Also protip: if you start the program off with `std::cout << std::boolalpha;`, then `cout` will display bools as 'true'/'false' instead of 1/0. – scohe001 Nov 08 '21 at 15:52
  • By the way all of your "loops" are only going to iterate once: `for (int a = 0; a < 1; a++)` `a` will only have the value of 0 in the loop. Once it increments to 1 it will exit the loop since `1 < 1` is false. – Kevin Nov 08 '21 at 15:55
  • Create a [mcve] – eerorika Nov 08 '21 at 16:02

1 Answers1

7

The culprit here is the part you didn't posted: using namespace std;

When you do that, your code will break as soon as you introduce a name that the namespace std already declares. In that case, it declares struct conjunction and struct disjunction.

Here's the minimal repro:

#include <type_traits>
using namespace std;
bool conjunction(bool, bool);
int main() { conjunction(true, true); }

Instead, simply remove using namespace std;, and use std::cout instead.

For more example of why using the std namespace is a bad idea, read Why is "using namespace std;" considered bad practice?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • Thank you! It works now :)) –  Nov 08 '21 at 16:02
  • @AnnaNiko No problem! My pleasure to help – Guillaume Racicot Nov 08 '21 at 16:02
  • sry nitpick, the minimal repro has to call the function, also with `using namespace std;` one could still call `::conjunction` without issues – 463035818_is_not_an_ai Nov 08 '21 at 16:19
  • @463035818_is_not_a_number Fixed, thanks. – Guillaume Racicot Nov 08 '21 at 16:45
  • @GuillaumeRacicot For some reason your program works [here](https://onlinegdb.com/MM_okL0dI) without giving any errors. – Jason Nov 08 '21 at 18:09
  • @AnoopRana you need `std::conjunction` to exist. It has been added in C++17 I think. Try using up to date tools. Look at this [compiler explorer link](https://godbolt.org/z/rWe653GMe), where the GCC version is up to date enough to enable C++17 by default. – Guillaume Racicot Nov 08 '21 at 18:47
  • @AnoopRana Limit yourself to C++14 [and it works](https://godbolt.org/z/xWz7a1eso). This is also where `using namespace std` is bad. You will get more and more name collision as the namespace you use adds more of them, like `std` is expected. – Guillaume Racicot Nov 08 '21 at 18:49
  • @GuillaumeRacicot Yeah i already knew that `using namespace std` is bad. Was just wondering why/how it worked on my given link. Now i know it works for C++14 which also means the link to the site that i have given must be using C++14. – Jason Nov 09 '21 at 04:21