0

The error is on line 76 int res[mSize]; the problem is on mSize. It seems like a simple fix but I can't figure it out. If someone can figure it out or point me in the right direction that would be greatly appreciated.

Also, the deconstructor ~MyContainer(), I am not sure if I am using it right or if there is a correct place to put it.

Here is my code:

#include <iostream>

using namespace std;

class MyContainer
{
private:
    int* mHead; // head of the member array
    int mSize;  // size of the member array

public:
    MyContainer();
    MyContainer(int*, int);
    //~MyContainer();
    void Add(int);
    void Delete(int);
    int GetSize();
    void DisplayAll();
    int FindMissing();
    ~MyContainer() {}
};

MyContainer::MyContainer()
{
    mHead = NULL;
    mSize = 0;
}

MyContainer::MyContainer(int* a, int b)
{
    mHead = a;
    mSize = b;
}

void MyContainer::Add(int a)
{
    *(mHead + mSize) = a;
    mSize++;
}

void MyContainer::Delete(int a)
{
    int index;
    for (int i = 0; i < mSize; i++)
    {
        if (*(mHead + i) == a)
        {
            index = i;
            break;
        }
    }

    for (int i = index; i < mSize; i++)
    {
        *(mHead + i) = *(mHead + i + 1);
    }
    mSize--;
}

int MyContainer::GetSize()
{
    return mSize;
}

void MyContainer::DisplayAll()
{
    cout << "\n";
    for (int i = 0; i < mSize; i++)
    {
        cout << *(mHead + i) << " ";
    }
}

int MyContainer::FindMissing()
{
    int res[mSize];
    int temp;
    int flag = 0;
    for (int i = 1; i <= mSize; i++)
    {
        flag = 0;
        for (int j = 0; j < mSize; j++)
        {
            if (*(mHead + j) == i)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0)
        {
            temp = i;
            break;
        }
    }
    return temp;
}


int main() 
{
    const int cSize = 5; 
    int lArray[cSize] = { 2, 3, 7, 6, 8 }; 
    MyContainer lContainer(lArray, cSize); 
    lContainer.DisplayAll(); 
    lContainer.Delete(7); 
    lContainer.DisplayAll(); 
    cout << "Size now is: " << lContainer.GetSize() << endl; lContainer.Add(-1); 
    lContainer.Add(-10); 
    lContainer.Add(15); 
    lContainer.DisplayAll(); 
    cout << "Size now is: " << lContainer.GetSize() << endl; 
    cout << "First missing positive is: " << lContainer.FindMissing() << endl;
    system("PAUSE"); return 0;
}
codesnerd
  • 767
  • 2
  • 8
  • 23
  • Arrays need to have a constant size, so you can't choose the size at runtime. Using a `std::vector` will solve that issue. – cigien Sep 24 '21 at 00:02
  • In `MyContainer::MyContainer(int* a, int b)`, who [owns](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) the allocation pointed to by `a`? This is a very important thing to work out before you can start on the destructor. If you decide you need a destructor, then you need to read about [the Rule of Three](https://en.cppreference.com/w/cpp/language/rule_of_three) and friends. – user4581301 Sep 24 '21 at 00:04
  • More closely related, what is `int res[mSize];` for? It seems to go unused, and I can't think of what I'd use it for. You can't return an array that's a local variable, it [decays to a pointer](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) and then goes out of scope leaving the caller with a timebomb, so you can't even use it to track multiple missing numbers. – user4581301 Sep 24 '21 at 00:11
  • What does '`this`' have to do with it? And what is your question about the destructor? – user207421 Sep 24 '21 at 06:41

1 Answers1

0

int res[mSize];

The size of the array mSize must be known at compile time. You cannot use a variable here. An option may be to define a macro with an largish value that will not exceeded.

static const int kLargeSize =100;
int res[kLargeSize];

Edited in response to the comments - const and constexpr are a better option than a macro.

Or even better, you can use std::vector - https://en.cppreference.com/w/cpp/container/vector

moi
  • 467
  • 4
  • 19
  • 1
    No good reason to use a macro when a constant can be used. – Phil1970 Sep 24 '21 at 00:20
  • Its better programming practice, as the codebase grows, these "magic numbers" tend to pop up everywhere. If there is a macro, these can be tracked and easily modified. However, as noted, std::vector is a better option. – moi Sep 24 '21 at 00:22
  • From the link = `Macros are just like any other tool ` Note that I have stated std::vector as the better solution. – moi Sep 24 '21 at 00:30
  • The code in question has multiple logic problems. In this particular case, the solution given fixes the problem that was listed. And, as stated earlier, std::vector is the better solution. – moi Sep 24 '21 at 00:37
  • `/*static*/` `constexpr` variable does the job. It is not a MACRO job here. – Jarod42 Sep 24 '21 at 07:56
  • I've modified the answer. to avoid further debate and because const and and constexpr are better options. – moi Sep 24 '21 at 12:07