我有 C++ 中的程序,它执行以下操作:获取一个未排序的整数数组并将其按升序划分为子数组。例如。我的数组是 1,2,3,-2,4 所以输出将是: 1,2,3 //子数组 1 -2,4 //子数组 2
我需要这样做的方法是分配指针数组,并且该数组中的每个单元格将指向包含一个系列的子数组(也是动态分配的)。另外,我不允许更改“主要”功能
现在,我有使用运算符重载打印子数组的“打印”方法。
我检查并发现打印方法工作正常,但在这段代码中:
cout << "\ns3: \n";
print(s3);
s3 = s1;
cout << "\ns3 again: \n";
print(s3);
print(s3) 打印得很好,但是在 print(s3) 再次调用时赋值运算符之后,我得到一个垃圾输出。经过一些调试后,我认为这与析构函数有关。
如果有人能指出并确定我的问题并提出解决方案,我将非常感激。当然我会给出源代码
更新
我想更好地解释一下自己:在每次调用“print”之后,当我追溯时,在“print”之前执行的第一件事就是复制构造函数。当我在复制构造函数中设置断点时,我看到函数签名(复制构造函数)中的指针“src”指向正确的位置(数组中的第一个元素),但是在我赋值 s3=s1 之后再次打印(s3)复制构造函数中的指针是垃圾
**
这里的代码:
// HW4.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
//global declerations and indexes//
typedef long int * LONG_INT_PTR;
//class
class SeriesArray
{
//members declerations
private:
LONG_INT_PTR *stable;
int *count_arr; //help array that indicate how many serieses made from the array and how many element in each series
int count_size; // help counter
int size;
public:
SeriesArray::SeriesArray(LONG_INT_PTR arr, int n );
SeriesArray::SeriesArray(const SeriesArray& src );
SeriesArray::~SeriesArray();
long int SeriesArray::get_entry(int i,int j) const;
SeriesArray& SeriesArray::operator=(const SeriesArray& src);
friend ostream& operator<<(ostream& stream, const SeriesArray& src);//global - not member
};
//members implementation
//default constructor
SeriesArray::SeriesArray(LONG_INT_PTR arr, int n )
{
size=n;
int j=0;
int s=0;
count_size=0;
count_arr=new int[size];
for (int i=0;i<size;i++)
{
if (arr[i]<arr[i+1])
count_size++;
else
{
count_size++;
count_arr[j]=count_size;
j++;
count_size=0;
}
}
size=j;
stable=new LONG_INT_PTR[size];
for (int i=0;i<size;i++)
{
stable[i]=new long int[count_arr[i]];
for (int k=0;k<count_arr[i];k++)
{
stable[i][k]=arr[s];
s++;
}
}
}
//copy constructor
SeriesArray::SeriesArray(const SeriesArray& src )
{
size=src.size;
count_arr=new int[size];
stable=new LONG_INT_PTR[size];
for (int i=0;i<size;i++)
{
count_arr[i]=src.count_arr[i];
stable[i]=new long int[count_arr[i]];
}
memcpy(this->stable,src.stable,src.size*sizeof(long int));
}
//destructor
SeriesArray::~SeriesArray()
{
for (int i=0;i<size;++i)
delete[] stable[i];
delete[] this->count_arr;
count_size=0;
}
//member
long int SeriesArray::get_entry(int i,int j) const
{
if (i<this->count_size&&this->stable[i][j]!=NULL)
return (this->stable[i][j]);
else return NULL;
}
//overload = operator
SeriesArray& SeriesArray::operator=(const SeriesArray& src)
{
if (this==&src)
return *this;
if (stable!=NULL)
delete[] stable;
if (count_arr!=NULL)
delete[] count_arr;
//for (int i=0;i<size;i++)
// delete[] stable[i];
size=src.size;
count_arr=new int[src.size];
memcpy(count_arr,src.count_arr,src.size*sizeof(int));
stable=new LONG_INT_PTR[size];
for (int i=0;i<size;i++)
{
// count_arr[i]=rhs.count_arr[i];
stable[i]=new long int[src.count_arr[i]];
}
memcpy(stable,src.stable,src.size*sizeof(long int));
return *this;
}
//***************************************************************************************************************************************************//
//global//
//overload operator <<
ostream& operator<<(ostream& stream,const SeriesArray& src)
{
for (int i=0;i<src.size;i++)
{
for (int j=0;j<src.count_arr[i];j++)
{
stream << " " << src.stable[i][j] << " " ;
}
stream << "\n\n\n" ;
}
return stream;
}
//print
void print(SeriesArray src)
{
cout << src;
cout << "\n";
} // print
//main
int main()
{
long int arr1[20] = {23, 91, -71, -63, 22, 55, 51, 73, 17, -19,
-65, 44, 95, 66, 82, 85, 97, 30, 54, -34};
long int arr2[10] = {0, 1, -7, -6, 2, 5, 6, 7, 1, -1};
int count[20], i, j, n =20, sno;
long int *parr[20];
SeriesArray s1(arr1, 20);
SeriesArray s2(arr2, 10);
SeriesArray s3(arr2, 10);
cout << "\narr1:\n";
for(i=0; i < 20; i++)
cout << " " << arr1[i] << " ";
cout << "\n";
cout << "\n\ntable:\n";
print(s1);
cout << "\narr2\n";
for(i=0; i < 10; i++)
cout << " " << arr2[i] << " ";
cout << "\n";
cout << "\n\ntable:\n";
print(s2);
cout << "\ns3: \n";
print(s3);
s3 = s1;
cout << "\ns3 again: \n";
print(s3);
cout << "\ns1 again: \n";
print(s1);
cin>>i;
return 0;
} // main