注册 登录
编程论坛 C++ Builder

C ++運算符重載錯誤:使用動態數組時出現雙重釋放或損壞(fasttop)

adssc 发布于 2020-05-22 08:01, 2153 次点击
我是c ++重載運算符的新手,所以可能會有很多語法錯誤。
不知道這是什麼問題,在函數聲明或實現中,該如何解決或提出寶貴意見?謝謝
下面的代碼給我一個錯誤:雙重釋放或損壞(fasttop):
輸入文件:
程序代码:
6
*
2 2 3
2 3 3
+
2 -5 -5
2 10 10
-
3 9 9 9
3 1 1 1
*
2 2 3
2 3 3
L
2 3 4
N
2 3 4


main.cpp
程序代码:
#include <iostream>
#include <fstream>
#include "Vector.h"

using namespace std;
int main()
{

    ifstream inFile("input.txt");
    if (!inFile)
    {
        cout << "Read input.txt error" << endl;
        exit(1);
    }

    int testCase;
    inFile >> testCase;
    while (testCase > 0)
    {
        char op;
        inFile >> op;

        Vector v1, v2, v3;

        switch (op)
        {
            case '+':
                cout << "v1 + v2" << endl;
                inFile >> v1;
                inFile >> v2;
                cout << "v1:" << v1 << endl;
                cout << "v2:" << v2 << endl;
                v3 = v1 + v2;
                cout << v3 << endl;
                break;
            case '-':
                cout << "v1 - v2" << endl;
                inFile >> v1;
                inFile >> v2;
                cout << "v1:" << v1 << endl;
                cout << "v2:" << v2 << endl;
                v3 = v1 - v2;
                cout << v3 << endl;
                break;
            case '*':
                cout << "v1 * v2" << endl;
                inFile >> v1;
                inFile >> v2;
                cout << "v1:" << v1 << endl;
                cout << "v2:" << v2 << endl;
                cout << v1 * v2 << endl;
                break;
            case 'L':
                cout << "Length of v1" << endl;
                inFile >> v1;
                cout << "v1:" << v1 << endl;
                cout << v1.length() << endl;
                break;
            case 'N':
                cout << "Normalize of v1" << endl;
                inFile >> v1;
                cout << "v1:" << v1 << endl;
                cout << v1.normalize() << endl;
                break;
            default:
                break;
        }
        cout << endl;
        testCase--;
    }

    return 0;
}


vector.cpp
程序代码:
#include "Vector.h"
#include <string>
#include <math.h>

Vector::Vector()
{
    currentIndex;
    arrSize = 3;
    arr = new float[arrSize];
}

Vector::Vector(int size)
{
   arrSize = size;
   arr = new float[arrSize];
}


Vector::~Vector()
{
    delete [] arr;
}

void Vector::AddNumbertoArr(float number)
{
   for(int i = 0; i < arrSize; i++)
   {
       arr[i] = number;
   }
}

int Vector::getSize()
{
   return arrSize;
}

float Vector::length()
{
   float sum=0;
   for(int i = 0; i < arrSize; i++)
   {
       sum+=arr[i]*arr[i];
   }
   return sqrt(sum);
}

Vector Vector::normalize()
{
   float total = length();
   for(int i = 0; i < arrSize; i++)
   {
      arr[i] / total;
   }
   return Vector(3);
}


Vector & Vector::operator+(Vector v)
{
   if(arrSize == v.arrSize)
   {
       for(int i = 0; i < arrSize; i++)
       {
           arr[i] += v.arr[i];
       }
       return *this;
   }
   else
   {
       cout << "Error: size not match";
   }
}

Vector & Vector::operator=(Vector v)
{
   if(arrSize == v.arrSize)
   {
       for(int i = 0; i < arrSize; i++)
       {
           arr[i] = v.arr[i];
       }
       return *this;
   }
   else
   {
       cout << "Error: size not match";
   }
   
}

Vector & Vector::operator-(Vector v)
{
   if(arrSize == v.arrSize)
   {
       Vector currentIndex(arrSize);
       for(int i = 0; i < arrSize; i++)
       {
           arr[i] -= v.arr[i];
       }
       return *this;
   }
   else
   {
       cout << "Error: size not match";
   }
}


float & Vector::operator*(Vector v)
{
   float count;
   if(arrSize == v.arrSize)
   {
        for(int i = 0; i < arrSize; i++)
        {
            count += this->arr[i] * v.arr[i];
        }   
        return count;
   }
   else
   {
      cout << "Error: size not match";
   }
   
}

ifstream & operator>>(ifstream & in, Vector &v)
{
   for(int i = 0; i < v.arrSize; i++)
   {
       in >> v.arr[i];
   }
   return in;
}

ostream & operator<<(ostream & out, const Vector & v)
{
   for(int i = 0; i < v.arrSize; i++)
   {
       out << v.arr[i];
   }
   return out;
}


Vector.h
程序代码:
#pragma once
#include <iostream>
#include <fstream>

using namespace std;

class Vector
{
   private:
       float *arr;
       int currentIndex;
       int arrSize;
   public:
       //constructor
       Vector();
       Vector(int size);
       ~Vector();
      
       //setter
       void AddNumbertoArr(float number);

       //getter
       int getSize();

       float length();
       Vector normalize();

       //overload
       Vector &operator +(Vector v);
       Vector &operator =(Vector v);
       Vector &operator -(Vector v);

       //dot product
       float &operator *(Vector v);

       //input output overload
       friend ifstream& operator >> (ifstream& in, Vector &v);
       friend ostream& operator << (ostream& out, const Vector &v);
};


範例輸出:
只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2020-5-22 10:40编辑过]

0 回复
1