所以我有班级IntArray
。这就是它的名字所说的,带有边界检查的动态整数数组类和那种东西。这是该类的方法和变量。
public:
IntArray();
IntArray(unsigned int length);
IntArray(const IntArray& ia);
~IntArray();
int length() const { return size; }
void resize(unsigned int length);
void insertBefore(int value, int index);
int& operator[] (int i);
IntArray& operator=(const IntArray& ia);
friend ostream& operator<< (ostream& out, const IntArray& array);
private:
unsigned int size;
unsigned int actualSize;
int * data;
const static int DEFAULT = 0; // Default fill value
const static int INCREMENT = 20; // Default value to increase size with
void fillWithDefault(int end, int start = 0);
void copyElements(int *toArray, int end, int start = 0, int offsetSize = 0, int offsetIndex = 0) const;
void resize(unsigned int length, unsigned int offset);
以下是主要代码:
IntArray a (6);
for ( int i =0; i <6;++ i ){
a [ i ]= i * i ;
}
cout << "BEFORE C" << endl;
IntArray c;
cout << "AFTER C" << endl;
c = a;
我已经测试过每一个函数并且它有效,除非我启动此代码时它永远不会到达IntArray c;
行。
错误:此应用程序已请求Runtime以不寻常的方式终止它。
额外的怪癖:在调试中运行,一切都很好。
编辑:额外代码
IntArray::IntArray() {
size = 0;
actualSize = INCREMENT;
data = new int[actualSize];
}
IntArray::IntArray(unsigned int length)
: size(length), actualSize(length) {
data = new int[length];
fillWithDefault(length);
}
IntArray::IntArray(const IntArray& ia)
: size(ia.size), actualSize(ia.actualSize) {
data = new int[actualSize];
ia.copyElements(data, size);
}
IntArray::~IntArray() {
delete [] data;
}
void IntArray::resize(unsigned int length) {
resize(length, length < size? length:size);
}
void IntArray::insertBefore(int value, int index) {
resize(size+1, index);
data[index] = value;
}
void IntArray::resize(unsigned int length, unsigned int offset) {
unsigned int tempSize = size;
unsigned int startIndex = offset; // Where to increase the array?
int * theData = data;
size = length;
if(length > actualSize) { // Entire array needs to be copied (new data!)
actualSize = length + INCREMENT;
theData = new int[actualSize];
startIndex = 0;
}
if(length > tempSize)
copyElements(theData, tempSize, startIndex, (length-tempSize), offset);
if(length > actualSize) {
delete [] data; // Delete former data and assign new
data = theData;
}
if(length > tempSize) // Fill remainder with DEFAULT
fillWithDefault(length, offset);
}
int& IntArray::operator[] (int i) {
if(i < 0 || (unsigned int)i >= size)
throw "\nIndex out of bounds\n";
return data[i];
}
IntArray& IntArray::operator=(const IntArray& ia) {
if(this != &ia) {
size = ia.size; // Copy variables
actualSize = ia.actualSize;
delete [] data; // Get rid of old data
data = new int[actualSize]; // Create new data and copy elements
ia.copyElements(data, size);
}
return *this;
}
ostream& operator<< (ostream& out, const IntArray& array) {
int size = array.size;
for(int i = 0; i < size; ++i) {
out << array.data[i];
if(i < size-1)
out << ' ';
}
return out;
}
void IntArray::fillWithDefault(int end, int start) {
if(start < 0 || start > end || (unsigned int)end > size)
throw "\nArray can't be filled with invalid indexes\n";
for(int i = start; i < end; i++)
data[i] = DEFAULT;
}
void IntArray::copyElements(int *toArray, int end, int start, int offsetSize, int offsetIndex) const {
if(start < 0 || start > end || (unsigned int)end > size)
throw "\nArray can't be filled with invalid indexes\n";
int offset = 0;
for(int i = start; i < end; ++i) {
if(i == offsetIndex)
offset = offsetSize;
toArray[i+offset] = data[i];
}
}