-2

我的问题很简单(双重免费或腐败(fasttop):)并且有一个解决方案(这里)。但是,我无法实现它。我不知道问题出在哪里。当我跟踪我的程序时,我看到它发生在二元运算符执行之后......作为一个例子;

arr1 = arr2 + arr3;

执行此语句后,它调用析构函数。所以我的析构函数将调用作用域的结尾,以便调用它比需要的多。我的班级定义是;

class IntArray
{
public:
    IntArray(int size);
    //IntArray(const IntArray& ref);
    ~IntArray();
    IntArray& operator= (const IntArray&);
    const int& operator[] (int index) const;
    friend ostream& operator <<(ostream&, IntArray);
    friend istream& operator >>(istream&, IntArray);
    friend IntArray operator *(const IntArray&, const IntArray&);
    friend IntArray operator +(const IntArray&, const IntArray&);
    friend IntArray operator -(const IntArray&, const IntArray&);
    friend bool operator ==(const IntArray&, const IntArray&);
    friend bool operator <(const IntArray&, const IntArray&);
    friend bool operator >(const IntArray&, const IntArray&);
    const int& getArr(const int index) const{
        return arr[index];
    }
    void setArr(int value, int index){
        arr[index] = value;
    }
private:
    int* arr;
    int sz;
};

我的析构函数是;

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

还有我的一个二进制重载函数;

 IntArray operator- (const IntArray& refOne, const IntArray& refTwo){
        IntArray newArray(refOne.sz);
        int value;
        for(int i = 0; i < refOne.sz-1; i++){
                value = refOne.getArr(i) - refTwo.getArr(i);
                newArray.setArr(value, i);
            }
        return newArray;
    }

无论是否需要,这里是我的构造函数;

IntArray::IntArray(int size){
    sz = size;
    arr = new int[size];
    for (int i = 0;i < size; i++)
        arr[i] = 0;
}

我没有使用复制构造函数。相反,我使用了赋值运算符。我的所有代码都是;

/*
 * sampleArrayImplemtation.cpp
 *
 *  Created on: Jul 31, 2012
 *      Author: musbuntu
 */

#include <iostream>
#include <cstdlib>
using namespace std;
class IntArray
{
public:
    IntArray(int size);
    //IntArray(const IntArray& ref);
    ~IntArray();
    IntArray& operator= (const IntArray&);
    const int& operator[] (int index) const;
    friend ostream& operator <<(ostream&, IntArray);
    friend istream& operator >>(istream&, IntArray);
    friend IntArray operator *(const IntArray&, const IntArray&);
    friend IntArray operator +(const IntArray&, const IntArray&);
    friend IntArray operator -(const IntArray&, const IntArray&);
    friend bool operator ==(const IntArray&, const IntArray&);
    friend bool operator <(const IntArray&, const IntArray&);
    friend bool operator >(const IntArray&, const IntArray&);
    const int& getArr(const int index) const{
        return arr[index];
    }
    void setArr(int value, int index){
        arr[index] = value;
    }
private:
    int* arr;
    int sz;
};
IntArray& IntArray::operator= (const IntArray& ref){
    if(this!=&ref){
        delete[] arr;
        sz = ref.sz;
        arr = new int[sz];
        for (int i=0; i<sz;i++)
            arr[i] = ref.arr[i];
    }
    return *this;
}
bool operator< (const IntArray& valOne, const IntArray& valTwo){
    int flag(0);
    for(int i = 0;i<valOne.sz;i++)
    {
        if (valOne[i] < valTwo[i]){
            flag = 1;
        }
        else{
            return(0);
        }
    }
    return(flag);
}
bool operator> (const IntArray& valOne, const IntArray& valTwo){
    int flag(0);
    for(int i = 0;i<valOne.sz;i++)
    {
        if (valOne[i] > valTwo[i]){
            flag = 1;
        }
        else{
            return(0);
        }
    }
    return(flag);
}

bool operator== (const IntArray& valOne, const IntArray& valTwo){
    int flag(0);
    for(int i = 0;i<valOne.sz;i++)
    {
        if (valOne[i] == valTwo[i]){
            flag = 1;
        }
        else{
            return(0);
        }
    }
    return(flag);
}
IntArray operator- (const IntArray& refOne, const IntArray& refTwo){
    IntArray newArray(refOne.sz);
    int value;
    for(int i = 0; i < refOne.sz-1; i++){
            value = refOne.getArr(i) - refTwo.getArr(i);
            newArray.setArr(value, i);
        }
    return newArray;
}
IntArray operator+ (const IntArray& refOne, const IntArray& refTwo){
    IntArray newArray(refOne.sz);
    int value;
        for(int i = 0; i < refOne.sz-1; i++){
            value = refOne.getArr(i) + refTwo.getArr(i);
                newArray.setArr(value, i);
            }
    return newArray;
}
IntArray operator* (const IntArray& refOne, const IntArray& refTwo){
    IntArray newArray(refTwo.sz);
    int value;
        for(int i = 0; i < refOne.sz-1; i++){
                value = refOne.getArr(i) * refTwo.getArr(i);
                newArray.setArr(value, i);
            }
    return newArray;
}
istream& operator>> (istream& inputStream, IntArray ref){
    cout << "Enter one by one with respect to a comma> ";
    for(int i = 0; i < ref.sz; i++){
        inputStream >> ref.arr[i];
    }
    return inputStream;
}
ostream& operator<< (ostream& outStream, IntArray ref){
    outStream << "\nYour array has size = " << ref.sz << " and all members are:" << endl;
    for (int i = 0; i < ref.sz; i++){
        outStream << ref[i] << "\t";
    }
    return outStream;
}
const int& IntArray::operator[] (int index) const{
    if (index >= 0 && index < sz){
        return(arr[index]);
    }
    else{
        cerr << "Index out of range, therefore use vectors instead." << endl;
    }
//  return(arr[1]);
}
IntArray::IntArray(int size){
    sz = size;
    arr = new int[size];
    for (int i = 0;i < size; i++)
        arr[i] = 0;
}
//IntArray::IntArray(const IntArray& ref){
//  arr = new int[ref.sz];
//  for(int i=0;i<ref.sz;i++)
//      arr[i] = ref.arr[i];
//}
IntArray::~IntArray(){
    delete[] arr;
}

int main(){
    int sz;
    cout << "Enter size of array> ";
    cin >> sz;
    IntArray arr1(sz);
    arr1.setArr(5, 3);
    IntArray arr2(sz);
    //arr2 = arr1;
    IntArray arr3(sz), arr4(sz), arr5(sz), arr6(sz);
    arr2.setArr(2, 1);
    arr6 = arr1;
    cout << arr6;
    arr3 = arr2 - arr1;
    arr4 = arr1 * arr2;
    arr5 = arr2 + arr1;
    if (arr6 == arr5)
        cout << "It's working.";
    if (arr2 < arr5)
        cout << "It's working, too";
    if (arr4 > arr2)
        cout << "It is also working.";
    cout << arr1 << arr2 << arr3 << arr4 << arr5 << arr6;
    return 0;
}

为了解决这个问题,我不得不使用深拷贝;并做到这一点;(根据维基百科文章:)首先添加这个库/标题:

#include <algorithm>

还有一个深拷贝构造函数:

IntArray(const IntArray& ref):sz(ref.sz), arr(new int[ref.sz]){
    copy(ref.arr, ref.arr + ref.sz, arr);
}

感谢您的耐心和建议...

4

1 回答 1

2

尊重三法则。否则,您所有的二进制操作都是错误的:

IntArray operator- (const IntArray& refOne, const IntArray& refTwo){
    IntArray newArray(refOne.sz);
    int value;
    for(int i = 0; i < refOne.sz-1; i++){
            value = refOne.getArr(i) - refTwo.getArr(i);
            newArray.setArr(value, i);
        }
    return newArray;
}

您按值返回 a IntArray,创建一个副本,并使用编译器生成的复制构造函数。如果您希望它正确运行,则必须实现一个复制构造函数。

如果不是将其注释掉:

public:
   IntArray(int size);
   //IntArray(const IntArray& ref);

您可以将其定义为私有:

public:
   IntArray(int size);
private:
   //IntArray(const IntArray& ref);

并且问题在编译时就已经很清楚了。

于 2012-08-04T18:54:14.090 回答