I'm trying to create a XOR operator and for an unknown reason my compiler doesn't accept bool xor()
as a function, neither does it allow me to call it or use it in any way possible.
I'd like to note that I'm following a book to learn C++. Specifically it's "Learn C++ From the Ground Up" by Herbert Schildt (3rd Edition) This piece of code is referenced in this book.
My code here works just fine if i name the function bool xar()
or bool XOR()
, but since I'm trying to learn C++ I'd like to gain some insight on why this error occurs.
#include <iostream>
using namespace std;
bool xor(bool a, bool b);
int main()
{
bool q, p;
cout << "Enter Q (0 or 1): ";
cin >> q;
cout << "Enter P (0 or 1): ";
cin >> p;
cout << "Q AND P: " << (q && p) << '\n';
cout << "Q OR P: " << (q || p) << '\n';
cout << "Q XOR P: " << xor(q, p) << "\n";
cout << "nice";
return 0;
}
bool xor(bool a, bool b)
{
return (a || b) && !(a && b);
} ```
// The error message i receive is from the lines:
// ---------------------------
// bool xor(bool a, bool b);
// *expected an identifier*
// ---------------------------
// cout << "Q XOR P: " << xor(q, p) << "\n";
// *expected an expression*
// ---------------------------
// bool xor(bool a, bool b)
// *expected an identifier*