簡體   English   中英

為什么代碼塊在“>>”和“=”重載后給我這個錯誤?

[英]Why does Code Blocks give me this error after “>>” and “=” overloading?

我正在學習 oop 並且我在一個小項目中工作,但是代碼塊給了我一個我無法解釋的錯誤。 “=”重載似乎看起來不錯,但如果我讀到 integer 之后我得到(0xC0000005)錯誤。 該項目是關於創建一個名為“Vector”(更像是一個數組)的 class 並創建一些構造函數/析構函數和運算符重載,僅此而已。


#include <iostream>
#include <stdlib.h>
using namespace std;


class Vector
{
    int *p;
    int dim;

public:

    Vector();

    Vector(int sizze, int val);

    Vector (Vector& v);

    void afiseaza();

    int suma();

    int maxpoz();

    void sorteaza();

    void reactualizare(int val, int nrcomponente);

    Vector& operator = (const Vector &v);

    friend std::ostream& operator<<(std::ostream&, Vector &v);

    friend std::istream& operator>>(std::istream&, Vector &v);

    void setVector(int sizze,int tablou[]);

    int getSize();

    int* getVector();

    int getElement(int i);

    ~Vector();


};



Vector :: Vector()
{
    this->dim=10;

    this->p=new int[this->dim];

    for (int i=0; i<this->dim; i++)
        (this->p)[i]=i;

}


Vector :: Vector(int sizze, int val)

{
    this->dim=sizze;
    this->p=new int[sizze];

    int i;

    for(i = 0; i < sizze; i++ )
        (this->p)[i]=val;



}

Vector :: Vector(Vector& v)
{
    this->dim=v.dim;

    this->p=new int[this->dim];

    for (int i = 0; i < dim; i++)
    {
        (this->p)[i]=v.p[i];

    }
}

Vector :: ~Vector()
{
    this->dim=0;

    delete[] this->p;

    this->p=NULL;


}

int Vector :: suma()
{

    int s=0,i;

    for(i=0; i<this->dim; i++)
        s+=(this->p)[i];


    return s;


}

int Vector :: maxpoz()

{

    int pozmax=0;
    int maxx=(this->p)[0];

    for ( int i=0; i<this->dim; i++)
        if (this->p[i]> maxx)
        {
            maxx=(this->p)[i];
            pozmax=i;
        }

    return pozmax;


}

void Vector :: sorteaza()
{

    int i,j;

    for (i=0; i<this->dim-1; i++)
        for (j=i+1; j<this->dim; j++)
            if ( (this->p)[i] > (this->p)[j])
            {
                (this->p)[i]=(this->p)[i]+(this->p)[j];
                (this->p)[j]=(this->p)[i]-(this->p)[j];
                (this->p)[i]=(this->p)[i]-(this->p)[j];
            }

}

void Vector :: reactualizare(int val, int nrcomponente)

{

    this->dim=nrcomponente;

    this->p=new int[this->dim];

    for (int i=0; i<(this->dim); i++)
    {
        (this->p)[i]=val;

    }


}


Vector& Vector::operator = (const Vector &v)
{

    if (this !=&v)

    {
        this->dim=v.dim;

        this->p=new int[this->dim];

        for(int i=0; i<=this->dim; i++)
        {
            (this->p)[i]=(v.p)[i];

        }

    }

}


std::ostream& operator<<(std::ostream&out, Vector &v)
{
    for  (int i=0; i<v.dim; i++)
        out<<(v.p)[i]<<" ";

    return out;

}

std::istream& operator>>(std::istream&in, Vector &v)
{

    std::cout<<"Enter the size of the vector(number of elements):\n";
    in>>v.dim;

    std::cout<<"\n";

    std::cout<<"Enter the elements:\n";

    v.p=new int[v.dim];

    for  (int i=0; i<v.dim; i++)
        in>>(v.p)[i];

    return in;
}

int* Vector:: getVector()
{
    return this->p;
}

int Vector:: getElement(int i)
{
    return (this->p)[i];

}

int Vector :: getSize()

{
    return this->dim;

}

void Vector :: setVector (int sizze, int tablou[])

{

    this->p=new int[sizze];
    this->dim=sizze;

    for (int i=0; i<this->dim; i++)
    {
        (this->p)[i]=tablou[i];
    }

}

int main()
{
    int a;

    Vector X,Y;

    cin>>Y;

    X=Y;

    cin>>a;

    return 0;


}

您的代碼中有一個錯誤。

Vector& Vector::operator = (const Vector &v)
{

    if (this !=&v)

    {
        this->dim=v.dim;

        this->p=new int[this->dim];

        for(int i=0; i<=this->dim; i++) // your bug here.  i should not reach to this -> dim. Change to : i < this->dim
        {
            (this->p)[i]=(v.p)[i];

        }

    }


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM