我的运营商+自定义Vector类出了什么问题?

时间:2016-06-23 11:29:46

标签: c++ class operator-overloading

我正在为教育目的做一个自定义Vector类,我无法正确使用 operator + 。它在返回一个值后触发断点,但我不知道为什么,也许是析构函数或构造函数,我不知道。

罕见的是,当我将operator+更改为返回T而不是Vector<T>,并返回thiscpy._els[i]时,程序运行正常。这就是我认为问题可能来自构造函数/析构函数的原因。

无论如何,这是 Vector.h 的相关部分:

#include <initializer_list>
#include <functional>

typedef size_t SIZE;

template <class T>
class Vector {
private:
    SIZE _sz;
    T *_els;
public:

    typedef std::function<void(Vector*)> sorting_function;

    static const void populateVector(Vector*, const SIZE&, typename Vector<T>::sorting_function, const bool& sameLength = false);

    Vector(SIZE sz = 0) : _sz(sz), _els(nullptr) {}
    Vector(SIZE, const T&);
    Vector(std::initializer_list<T>);
    Vector(const Vector&);
    Vector(Vector&& vec) : _sz(vec._sz), _els(vec._els) { delete[] vec._els; }

    ~Vector() { delete[] _els; }

    Vector& operator=(const Vector&);
    Vector& operator=(Vector&&);
    Vector& operator=(std::initializer_list<T>);
    Vector operator+(const Vector&);

    SIZE size() const { return _sz; }
    T* elems() const { return _els; }

    int *begin() { return &_els[0]; }   //  for (auto i : Vector) {}
    int *end() { return &_els[_sz]; }   //
};

这里我的相关部分 Vector.cpp

#include <stdexcept>
#include "Vector.h"


template <class T>
Vector<T>::Vector(const Vector& vec) {
    cout << "Vector initializer" << endl;
    if (this != &vec) {
        populateVector(this, vec._sz, [&](Vector<T>* obj) {
            for (SIZE i = 0; i < vec._sz; i++)
                obj->_els[i] = vec._els[i];
        });
    }
}

template <class T>
Vector<T>& Vector<T>::operator=(const Vector<T>& vec)
{
    cout << "Operator = const" << endl;
    populateVector(this, vec._sz, [&](Vector<T>* obj) {
        for (SIZE i = 0; i < vec._sz; i++)
            obj->_els[i] = vec._els[i];
    }, true);

    return *this;
}

template <class T>
Vector<T>& Vector<T>::operator=(Vector<T>&& vec)
{
    cout << "Operator = move" << endl;
    populateVector(this, vec._sz, [&](Vector<T>* obj) {
        for (SIZE i = 0; i < vec._sz; i++)
            obj->_els[i] = vec._els[i];
    });

    delete[] vec._els;

    return *this;
}

template <class T>
Vector<T>& Vector<T>::operator=(std::initializer_list<T> list)
{
    populateVector(this, list.size(), [&](Vector<T>* obj) {
        SIZE i = 0;
        for (T elem : list)
            obj->_els[i++] = elem;
    });

    return *this;
}

template <class T>
Vector<T> Vector<T>::operator+(const Vector<T>& vec)
{
    cout << "Operator + const" << endl;
    Vector<T> thiscpy(*this);

    if (_sz != vec._sz) throw std::runtime_error("Vector size mismatch");

    for (SIZE i = 0; i < _sz; i++)
        thiscpy._els[i] += vec._els[i];

    return thiscpy;
}

template <class T>
const void Vector<T>::populateVector(Vector<T>* obj, const SIZE& newsize, typename Vector<T>::sorting_function repopf, const bool& sameLength = false)
{
    cout << "Pupulate vector" << endl;
    if (sameLength && (obj->_sz != newsize)) throw std::runtime_error("Incompatible vector length");

    obj->_sz = newsize;
    try
    {
        if (obj->_els != nullptr) delete[] obj->_els;
        obj->_els = new T[newsize];

        repopf(obj);
    }
    catch (const std::exception& e)
    {
        obj->_sz = 0;
        obj->_els = nullptr;
        throw std::runtime_error("Couldn't populate vector");
    }
}

main.cpp

int main() {

    Vector<int> v1{ 1,2,3,4 };     //Vector<T>::Vector(std::initializer_list<T>);
    Vector<int> v2{ 2,4,8,16 };    //Vector<T>::Vector(std::initializer_list<T>);

    try
    {
        cout << "----------" << endl;
        v1 + v2;                       //Triggers breakpoint
        cout << "----------" << endl;
        cout << "done" << endl;
    }
    catch (const std::exception& e)
    {
        cout << e.what() << endl;
    }

    cin.get();
    return 0;
}

该程序的输出

    Pupulate vector
    Pupulate vector
    ----------
    Operator + const
    Vector initializer
    Pupulate vector

1 个答案:

答案 0 :(得分:2)

您的复制构造函数在调用els之前从不初始化populateVector,因此它可能不是nullptr(也可能是)并且您在任何内容上调用delete[]成员指针是。这可能导致未定义的行为。