Oct 26, 2018 at 3:16pm UTC
Your enum BOOL is not the same thing as the built-in C++ type bool
. As your compiler is telling you, you can't implicitly convert one to the other.
Why do you need that enum? Why can't you just use the built-in bool
type?
Oct 26, 2018 at 3:20pm UTC
its just an example in my book. I'm trying to learn about assert macro's. Can you show me how to make this compile with the built in bool? thanks.
Oct 26, 2018 at 3:41pm UTC
ok i tried my best with the inbuilt bool but its giving me 3 errors at the moment
line 74 to String::~String()
line 96 to Animal::Animal(int String const &)
to String::String char const*
to String::~String()
line 102 to String::~String()
thanks.
#include <iostream>
enum LEVEL { NONE,LOW,MEDIUM,HIGH };
//enum BOOL { FALSE,TRUE };
#define DEBUGLEVEL MEDIUM
#if DEBUGLEVEL < LOW
#define ASSERT(x)
#else
#define ASSERT(x)\
if(! (x))\
{\
std::cout <<"ERROR! Assert " << #x << " failed.\n";\
std::cout <<" on line " << __LINE__ << "\n";\
std::cout <<" in file " << __FILE__ << "\n";\
}
#endif
#if DEBUGLEVEL < MEDIUM
#define EVAL(x)
#else
#define EVAL(x)\
std::cout<<#x<<":\t"<<x << std::endl;
#endif
#if DEBUGLEVEL < HIGH
#define PRINT(x)
#else
#define PRINT(x)\
std::cout<<x<<std::endl;
#endif
class String
{
public:
// constructors
String();
String(const char * const);
String(const String &);
~String();
char & operator[](int offset);
char operator[](int offset) const;
String & operator = (const String&);
int GetLen()const {return itsLen;}
const char * GetString()const {return itsString;}
bool Invariants()const;
private:
String(int);
char * itsString;
unsigned short itsLen;
};
bool String::Invariants()const
{
PRINT("(String Invariants Checked)");
return((bool) (itsLen && itsString) || (!itsLen && itsString) );
}
class Animal
{
public:
Animal():itsAge(1),itsName("John Q. Animal"){ASSERT(Invariants());}
Animal(int, const String&);
~Animal(){}
int GetAge(){ASSERT(Invariants()); return itsAge;}
void SetAge(int Age){ASSERT(Invariants()); itsAge = Age;
ASSERT(Invariants());}
String & GetName() { ASSERT(Invariants()); return itsName;}
void SetName(const String & name)
{ASSERT(Invariants()); itsName = name; ASSERT(Invariants());}
bool Invariants();
private:
int itsAge;
String itsName;
};
bool Animal::Invariants()
{
PRINT(("Animal Invariants Checked"));
return (itsAge > 0 && itsName.GetLen());
}
int main()
{
const int AGE = 5;
EVAL(AGE);
Animal sparky(AGE,"Sparky");
std::cout<<"\n"<< sparky.GetName().GetString()<<" is ";
std::cout << sparky.GetAge()<< " years old.";
sparky.SetAge(8);
std::cout<<"\n"<< sparky.GetName().GetString()<< " is";
std::cout<< sparky.GetAge()<< " years old.";
}
Last edited on Oct 26, 2018 at 3:42pm UTC
Oct 26, 2018 at 4:13pm UTC
c++ is case aware. If you accidentally put string instead of String, it will give a pile of errors, similar to earlier where you may have mixed BOOL and bool. Watch out for this (and consider calling your own stuff myString or something obvious, one letter different only in case is a recipe for typo problems that may be hard to spot).