0

So I have a function called isEmpty() to check if a specified array has been filled with variables.

    bool isEmpty() const;

This is:

bool Array::isEmpty() const
{
   if(elemData == NULL)
        return true;
   else
        return false;
}

I'm trying to call it in my main.cpp so that I can send the output of isEmpty to cout, but I can't work out how to call it. I've tried a bunch of different methods but I feel I'm shooting in the dark, and I can't find any similar examples elsewhere.

How can I do this?

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
Joe of Loath
  • 169
  • 1
  • 4
  • 10

2 Answers2

3

You can directly use

std::cout << your_container.isEmpty();

Or you can even output it like:

std::cout << std::boolalpha << your_container.isEmpty();
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
2

You mean this?:

cout << array.isEmpty();

Booleans can be printed by default. In fact, every class you create can print out values with cout, but only if you previously define both ostream and istream operators for this class.

David G
  • 94,763
  • 41
  • 167
  • 253
  • foo.isEmpty(); gives me 'Expression must be a class type'. Is using Array foo(); wrong? – Joe of Loath Mar 28 '14 at 11:52
  • Don't forget you can have the boolean displayed as text: http://stackoverflow.com/questions/29383/converting-bool-to-text-in-c/29571#29571 – graham.reeds Mar 28 '14 at 11:52
  • 2
    @JoeofLoath, That's a completely different problem. [Here's what Clang says with warnings.](http://coliru.stacked-crooked.com/a/60eecbf4724a1400) – chris Mar 28 '14 at 11:53
  • OK, how do I fix it? Or should I start a new question? – Joe of Loath Mar 28 '14 at 11:54
  • First create an Array foo and initialize it properly. Since this function returns a boolean value, it should let you print it out via cout. Give it another try and let me know the results –  Mar 28 '14 at 11:55
  • @JoeofLoath, See the link. The compiler says in plain English what to do. – chris Mar 28 '14 at 11:55