我自己的矢量类

时间:2015-05-26 20:14:50

标签: c++ oop vector

我正在尝试创建自己的矢量类。我想重载STL向量的基本功能。我的代码在测试之后编译,显示消息“分段错误(核心转储)”。你能告诉我在哪里可以改进我的代码吗?提前谢谢!

#include<iostream>
#include<fstream>
using namespace std;

class Vector
{
 public:
    Vector();
    Vector(const Vector& vect);
    ~Vector();
    Vector& operator =(const Vector& vect);
    void insert(double element);
    bool operator ==(const Vector& vect);
    Vector& operator +(const Vector& vect);
    Vector& operator -(const Vector& vect);
    Vector& operator *(const Vector& vect);
    int size();
    double operator[](int num);
    friend istream& operator >>(istream& input,  Vector& vect);
    friend ostream& operator <<(ostream& output,  Vector& vect);
 private:
    double* data;
    int num;
    int max;
};

Vector:: Vector()
{
    double* data = NULL;
    int num = 0;
    int max = 0;
}

Vector::Vector(const Vector& vect)// not sure if its correct
{

    data = new double[vect.max];
    memcpy(data, vect.data, sizeof(double) * vect.num);
    num = vect.num;
    max = vect.max;

}

Vector::~Vector()
{
    if(data)
    {
        delete[] data;
        data = 0;
    }
}

void Vector:: insert(double element)
{
    if(num < max)
    {
        *(data + num) = element;
    }
    else
    {
        double *tmp = new double[num * 2];
        delete[] data;
        *(data + num) = element;
        max *= 2;
    }
    num++;
}

Vector& Vector:: operator = (const Vector& vect)// highly unlikely to be correct
{
    if(this != &vect)
    {
            if(data)
            {
                delete[] data;
                data = 0;
            }
            max = vect.max;
            data = new double[max];
            for(int i = 0; i < num; i++)
            {
                this->data[i] = vect.data[i];
            }

    }
    return *this;
}

bool Vector:: operator ==(const Vector& vect)
{
    bool isEqual = true;
    if(this->num != vect.num)
    {
        isEqual = false;
    }
    for(int i = 0; i < num; i++)
    {
        if(this->data[i] != vect.data[i] )
        {
            isEqual = false;
        }
    }
    return isEqual;
}

Vector& Vector:: operator +(const Vector& vect)
{
    Vector result;

    for (int i = 0; i < num; ++i)
    {
        result.insert(*(data + i) + *(vect.data + i));
    }

    return result;

}

Vector& Vector:: operator -(const Vector& vect)
{
    Vector result;

    for (int i = 0; i < num; ++i)
    {
        result.insert(*(data + i) - *(vect.data + i));
    }

    return result;

}

Vector& Vector::operator*(const Vector &vect)
{
    Vector result;

    for (int i = 0; i < num; ++i)
    {
        result.insert(*(data + i) * *(vect.data + i));
    }

    return result;
}

int Vector:: size()
{
    return num;
}

double Vector::operator[](int num)
{
    return *(data + num);
}

ostream &operator <<(ostream &output,  Vector &vect)
{
    output << "[";
    for (int i = 0; i < vect.size(); ++i)
    {
        output << vect[i];
        if (i < vect.size() - 1)
            output << ",";
    }
    output << "]\n";

    return output;
}

istream &operator >>(istream &input,  Vector &vect)
{
    double element;
    input >> element;
    vect.insert(element);

    return input;
}

int main()
{
    Vector vec;
    for (int i = 0; i < 10; ++i)
    {
        vec.insert(i);
    }


    return 0;
}

1 个答案:

答案 0 :(得分:0)

仔细查看您的insert功能。特别是在如何重新调整数组大小。