0

I have following problem:

~Set()
{
    delete _arraySet;
};

void addToSet(T val)
{
    if(_currLen < _maxLen)
    {
        _arraySet[_currLen] = val;
        _currLen++;
    }
}

Set<T> operator+(Set& other)
{
    Set result;

    for(int i=0; i< _currLen; i++)
    {
        result.addToSet(_arraySet[i]);
    }
    for(int i=0; i< other._currLen; i++)
    {
        result.addToSet(other._arraySet[i]);
    }

    return result;
}

After calling

Set<int> temp  = x + x;

in main, I get _BLOCK_TYPE_IS_VALID assert error. To be entirely honest I have no clue as to why it is happening, but after running debug it seems that somehow I delete result from operator+ immediately instead of performing an = operation on it. I'm sorry for not being able to provide any more details...

@Edit:

Looks like I've been missing the Rule of Three. Here's the link to the article for anyone with similar problem. Thanks a lot for help!

Community
  • 1
  • 1
Krzysztof Piszko
  • 1,355
  • 3
  • 12
  • 17
  • What is the type of _arraySet? – Borgleader Jan 07 '15 at 17:35
  • 1
    Your class probably has invalid copy semantics - make sure it follows the [Rule of Three](http://stackoverflow.com/questions/4172722). Also, it looks like `_arraySet` points to an array, so you need `delete[]` not `delete`. Better still, just use `std::vector` and save yourself a world of pain trying to reinvent it. – Mike Seymour Jan 07 '15 at 17:36
  • @MikeSeymour Yeah, I know - it's for my uni so I can't do anything about it. About [] after delete, don't know how I missed that one -- fixed. Going to look into RoT. Borgleaader - The type is T* – Krzysztof Piszko Jan 07 '15 at 17:39

0 Answers0