gpt4 book ai didi

c++ - 我的自定义 Vector 类的 operator+ 有什么问题?

转载 作者:行者123 更新时间:2023-11-28 02:04:43 24 4
gpt4 key购买 nike

<分区>

我正在做一个用于教育目的的自定义 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");
}
}

ma​​in.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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com