Why am I getting a segmentation fault?

I assume it's something simple that I'm doing wrong, but I'd appreciate the help. Here's the Set class that I used. My main function did nothing but test the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
using namespace std;

template <class T>
class Set
{
public:
    Set();
    Set(Set<T>& setObject);
    void add(T addedValue);
    T* getArray();
    int getQuantity();
    void increaseCapacity();
    ~Set();
private:
    vector<T> items;
    int quantity;
};

template <class T>
Set<T>::Set() : quantity(0)
{
}

template <class T>
Set<T>::Set(Set<T>& setObject) : quantity(setObject.getQuantity())
{
    items.clear();
    T* object = setObject.getArray();
    for(int i = 0; i < quantity; i++)
    {
        items.push_back(object[i]);
    }
}

template <class T>
void Set<T>::add(T addedValue)
{
    items.push_back(addedValue);
    quantity = items.size();
}

template <class T>
T* Set<T>::getArray()
{
    return &items[0];
}

template <class T>
int Set<T>::getQuantity()
{
    return this->quantity;
}

template <class T>
Set<T>::~Set()
{
    if(!items.empty())
    {
        delete [] &items;
    }
}
This is wrong delete [] &items;
The vector will clean up itself in the destructor so no need to do anything. Only delete what you create with new.
Last edited on
Thanks, I appreciate it. So is there even a reason to have a ~Set() function in this class?
no
A destructor will be made; if not by you, by the compiler. If you've got nothing special you want to do in it, don't bother making one yourself.
Understood. Thanks for the help, everyone.
Topic archived. No new replies allowed.