我的問題很容易(雙重釋放或腐敗(fasttop)),並沒有爲此(here)的解決方案。但是,我無法實現它。我不知道問題在哪裏。當我已經跟蹤我的節目,我看到它二元操作執行... 作爲一個例子後會發生;分配明確重載調用析構函數(其他二進制運算符重載執行後)
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;
}
爲了解決這個問題,我不得不使用深拷貝; 並做到這一點; (根據維基百科article:) 首先添加此庫/頭:
#include <algorithm>
和深拷貝構造函數:
IntArray(const IntArray& ref):sz(ref.sz), arr(new int[ref.sz]){
copy(ref.arr, ref.arr + ref.sz, arr);
}
感謝您的耐心和建議......
1.有什麼問題嗎? 2.實現複製構造函數。 – 2012-08-04 18:46:56
嗨@LuchianGrigore, 我想要實現分配超載。所以,我並不需要使用拷貝構造函數 – mustafaSarialp 2012-08-04 18:48:52
你的「析構函數」看起來並不像一個析構函數。 – juanchopanza 2012-08-04 18:49:33