| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2162 人关注过本帖
标题:C ++運算符重載錯誤:使用動態數組時出現雙重釋放或損壞(fasttop)
只看楼主 加入收藏
adssc
Rank: 2
等 级:论坛游民
威 望:1
帖 子:61
专家分:32
注 册:2018-4-20
结帖率:100%
收藏
 问题点数:0 回复次数:0 
C ++運算符重載錯誤:使用動態數組時出現雙重釋放或損壞(fasttop)
我是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编辑过]

搜索更多相关主题的帖子: Vector cout operator int float 
2020-05-22 08:01
快速回复:C ++運算符重載錯誤:使用動態數組時出現雙重釋放或損壞(fasttop)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042749 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved