我正在尝试将我的数组调整为更大的数组,但是当我尝试这样做时,我会为我的类调用复制构造函数。由于某种原因,它为传递的参数提供了错误的值。任何帮助,将不胜感激。
// ---------------------------------------------------------------------------
// Copy
// Copy constructor for class Array
IntSet::IntSet(const IntSet &init) {
cout << "IN COPY CONSTRUCTOR FOR: " << init << endl;
size = init.size;
count = init.count;
arr = new bool[size];
for (int i = 0; i < size; i++) {
arr[i] = init.arr[i];
}
}
bool IntSet::insert(int value) {
if(check(value)) {
cout << "START OF INSERT: " << *this << " VALUE: " << value << endl;
if(value >= size) {
cout << "INSIDE IF " << endl;
size = value + 1;
IntSet(*this);
}
cout << "AFTER VALUE >= SIZE " << *this << endl;
arr[value] = true;
count++;
cout << " AFTER ADDING: " << *this << " COUNT: " << count << endl;
return true;
}
return false;
}
我已经在整个代码中编写了调试打印,这是一个片段。
A = {}
Enter ints for set A:
9
START OF INSERT: {} VALUE: 9
INSIDE IF
IN COPY CONSTRUCTOR FOR: { 2 4 5 9}
IN DESTRUCTOR!!! FOR { 2 4 5 9}
AFTER VALUE >= SIZE { 2 4 5 9}
AFTER ADDING: { 2 4 5 9} COUNT: 1
2
START OF INSERT: { 2 4 5 9} VALUE: 2
AFTER VALUE >= SIZE { 2 4 5 9}
AFTER ADDING: { 2 4 5 9} COUNT: 2
1
START OF INSERT: { 2 4 5 9} VALUE: 1
AFTER VALUE >= SIZE { 2 4 5 9}
AFTER ADDING: { 1 2 4 5 9} COUNT: 3
-1
A = { 1 2 4 5 9}
IN DESTRUCTOR!!! FOR {}
IN DESTRUCTOR!!! FOR { 1 2 4 5 9}
编辑:为了解释一下这个程序做了什么,它是一个试图将整数存储在布尔数组中的程序。