简体   繁体   English

动态布尔数组构造函数问题

[英]Dynamic Array of Booleans Constructor Issue

I'm having problems with the constructor of a class with an array of booleans, representing whether a int value is present by holding a "true" value at that index. 我在使用带有布尔数组的类的构造函数时遇到问题,该数组通过在该索引处保留“ true”值来表示是否存在int值。 So if I create a new set: 因此,如果我创建一个新集合:

IntSet set(4, 5, 6); // up to 5 parameters may be entered, with default values if some are missing.

this will create an array of size 6 (the highest value passed) and the value at indexes 4, 5 and 6 will be set to true, while the rest false. 这将创建一个大小为6(传递的最大值)的数组,并且索引4、5和6的值将设置为true,其余的设置为false。

This is what I have so far but I can't seem to get it to work properly: 这是我到目前为止的内容,但似乎无法正常工作:

.h: 。H:

class IntSet {

public:

bool* value;
int size;

// CONSTRUCTORS
IntSet(int val1 = -1, int val2 = -1, int val3 = -1, int val4 = -1, int val5 = -1);
IntSet(const IntSet &other);
~IntSet();

.cpp: .cpp:

IntSet::IntSet(int val1, int val2, int val3, int val4, int val5) {



// check if all values are default.
if((val1 == -1) && (val2 == -1) && (val3 == -1) && (val4 == -1) && (val5 == -1)) {

    val1 = 1;
    size = 1;

// else if one or more values are not default, find largest value and 
// set size to the value.
} else {

    if(val1 >= val2 && val1 >= val3 && val1 >= val4 && val1 >= val5) {
        size = val1;
    }

    if(val2 >= val1 && val2 >= val3 && val2 >= val4 && val2 >= val5) {
        size = val2;
    }

    if(val3 >= val1 && val3 >= val2 && val3 >= val4 && val3 >= val5) {
        size = val3;
    }

    if(val4 >= val1 && val4 >= val2 && val4 >= val3 && val4 >= val5) {
        size = val4;
    }

    if(val5 >= val1 && val5 >= val2 && val5 >= val3 && val5 >= val4) {
        size = val5;
    }
}

// Create pointer to boolean array of size "size"
value = new bool[size];

// set all values at their specified index to true.
if(val1 >= 0)
    value[val1] = true;

if(val2 >= 0)
    value[val2] = true;

if(val3 >= 0)
    value[val3] = true;

if(val4 >= 0)
    value[val4] = true;

if(val5 >= 0)
    value[val5] = true;
}

//------------------------------------------------------------------------------

IntSet::IntSet(const IntSet &otherSet) : size(otherSet.size){
cout << "In Copy Constructor" << endl;

value = new bool[size];
for(int i = 0; i < size; i++)
    value[i] = otherSet.value[i];
}

//------------------------------------------------------------------------------

IntSet::~IntSet() {
cout << "In Destructor" << endl;

delete[] value;
}

For some reason, when I debug and follow the values, I see "value" set to true(205). 由于某些原因,当我调试并遵循这些值时,我看到“值”设置为true(205)。 I checked by outputting the boolean values in the console and all values print as "205" instead of "0" or "1". 我通过在控制台中输出布尔值进行检查,所有值都打印为“ 205”而不是“ 0”或“ 1”。 I'm not sure what is going on, but I think the issue is with bool *value = new bool[size]; 我不确定发生了什么,但我认为问题出在bool * value = new bool [size]; although I've tried many different things and nothing seems to work. 尽管我尝试了许多不同的方法,但似乎没有任何效果。 I will tackle the copy constructor and destructor once I figure this out. 一旦弄清楚了,我将解决复制构造函数和析构函数。

This is a class assignment, so I'm not looking to get the solution from anyone, but any tips that may point me in the right direction are much appreciated! 这是一个课堂作业,因此我不希望从任何人那里获得解决方案,但是,可能会向正确的方向指出我的任何提示,我们将不胜感激!

Thanks everyone! 感谢大家!

In the constructor: 在构造函数中:

IntSet::IntSet(int val1, int val2, int val3, int val4, int val5);

You create a local variable called the same as the class variable "value", which hides the class declaration. 您创建一个与类变量“ value”相同的局部变量,该变量将隐藏类声明。

// Create pointer to boolean array of size "size"
bool *value = new bool[size];

Therefore, the class variable is still unitialized. 因此,类变量仍然是统一的。

Results: 结果:

  • local variable value causes memory leak 局部变量值导致内存泄漏
  • class variable never defined, will be improperly deleted - runtime error 类变量从未定义,将被不正确地删除-运行时错误

Instead use: 而是使用:

value = new bool[size];

or 要么

this->value = new bool[size];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM