1

Lets say I have an object array declared like this:

Object Array[100];
int count = 0;
bool exit;

do
{
    if (Array[count] == "")
    {
        //code that stores data
        exit = true;
    }
    else
    {
        count++;
    }
}
while (exit != true);

I keep having errors from the compiler which says:

error: no match for ‘operator==’ in ‘Array[count] == ""'

I know I can use a for loop function to store them correctly or even use a vector, but for now, I have to use this method to check if the array is empty/null. Any idea how to do it? I have seen many examples here, but almost all of it are string/int/float etc etc arrays.

3 Answers 3

1

The array itself cannot be "empty".

Object Array[100];

is a declaration that creates an array of 100 Objects. They are already constructed and present (if default constructible).

If the type Object has some notion of an "empty" state you could check for that (A std::vector for example provides the member .empty() to check for emptyness.) or you can use a container to hold your data (which also makes it part of the heap instead of the stack memory and enables dynamic resizing).

std::vector<Object> vec;
int count = 0;
// do stuff...
do
{
    if (count >= vec.size())
    {
        //code that stores data
        exit = true;
    }
    else
    {
        count++;
    }
}
while (exit != true);

Whereas you could also say std::size_t count = vec.size();.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, i did a bit of research on vector and manage to convert my arrays to vector, but after i hit compile, i face the problem whereby the compiler tells me there is no member named empty. I was using !vec[0].empty() to check if the vector is empty or not.
vec.empty() tells you if the vector itself is empty. vec[0].empty() is two expressions and calls empty() on the first element of the vector. Therefore the conent type of the vector needs to support empty(). If you want a structure that can have a filled element 6 but empty element 4 you need to go for one more level of indirection (pointers or smart pointers for example).
Meaning i cant just use the .empty itself ? cause i have a loop function that works something like vec[count].empty(). Then everytime it does the check and if all is fine, it will count++ and go to the next location to check.
If you want the number of elements in the vector just use vector.size().
1

When you created the array using: Object Array[100]; it already created 100 memory slots for your Objects, and called the default constructor for each one of them.

  • Physically, there are 100 elements in your array right after the quoted line.

  • Logically, there are as much as you decide, and its best you save that number to count.


If the Object's default c'tor is not enough in terms of initialization, then you have several options for checking weather it's content is initialized or not:

  • you can add a method (i.e. bool isInitialized())
  • you can add an operator== that takes const char * as an argument and compare it to "".
  • you can add an operator== that takes another Object as an argument and compare it to "" assuming Object has a c'tor from const char *.

Comments

0

Object Array[100]; cannot be empty. On the other hand, pointer to dynamically allocated array can be initialized to nullptr and checked against that:

Object *Array = nullptr;

if(!Array) // or Array == nullptr
    cout << "Array is empty (unallocated)" << endl;

Array = new Array[100];

if(Array) // or Array != nullptr
    cout << "Array is NOT empty" << endl;

Array[count] doesn't make sense. you are accessing element at index count (which is a bad name for index) and comparing that with const char*. std::vector is the way to go. Code above (and everything other than using std::vector) - ugly and bad practice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.