G
G
Gleb2020-05-18 22:58:34
C++ / C#
Gleb, 2020-05-18 22:58:34

What's wrong with the Vector class?

When compiling, I get: Process finished with exit code 11

template <typename T>
class Vect
{
private:
    T *pvector;
    
    size_t size, capacity;

public:
    Vect() : pvector(NULL), size(0), capacity(0) { }
    
    ~Vect() { delete[] pvector; }

    inline void push_back(const T &elem)
    {
        capacity;
        pvector[size] = elem;
        ++size;
    }

    inline void pop_back()
    {
        T result = pvector[size - 1];
        --size;
    }

    inline bool empty() const
    {
        return size == 0;
    }

    //возврат последнего элемента
     inline const T back() const
    {
        return pvector[size - 1] ;
    }
};

class Stack based
class Stack
{
private:
    Vect<T> elems; 
...

in int main() :
int main()
{
    setlocale(LC_ALL,"ru");

    Stack<int>  is;   

    is.push(7);
    is.push(9);
    cout<<is.top()<<'\n';

If I use the vector library, everything works fine, but if I create my own Vect class, nothing works, I guess I screwed up somewhere in Vect, tell me where exactly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-05-18
@shevzoom

I guess I messed up somewhere in Vect, tell me where exactly?

I screwed up here:
inline void push_back(const T &elem)
    {
        capacity;
        pvector[size] = elem;
        ++size;
    }

You forgot to allocate memory for pvector, which is why you got a segfault.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question