简体   繁体   中英

Why is my sequence constructor not working properly?

I am currently working on a project in C++ where I must create a sequence and then perform functions on it. However, I have come across an issue with my constructor and I can't understand what is wrong with it. A new sequence does not get created. I am using an array to implement my sequence. Also, I believe the issue might be in my insert() function as well.

Here is my constructor:

Sequence::Sequence(size_type sz)
{
    numElts = sz;
    elts = new int[sz];
}

This is my insert function:

void Sequence::insert(size_type position, value_type value)
{
    for (int i = 0; i <= numElts; i++) {
        elts[i];

        if (i = position) {
            elts[i] = value;
        }
    } 
}

Your constructor is allocating an array just fine (make sure the rest of your class implements the Rule of 3/5/0 properly, though), but it is not populating the array with any initial values.

Your insert() , on the other hand, has several problems:

  • it is looping through 1 too many elements. Arrays are 0-indexed, meaning the valid indexes will be 0..sz-1 , so you need to use < instead of <= in the loop.

  • elts[i]; doesn't really do anything meaningful. It is just reading a value from the array (in the case where i is the same value as numElts , it will read past the end of the array into surrounding memory), but it is not doing anything with that value.

  • if (i = position) assigns the value of position to i , and then evaluates the result of that assignment (the new value of i ) as a boolean expression. So, if position is 0 then nothing happens at all since 0 evaluates as false, but any other value will evaluate as true and every loop iteration (including the 1 erroneous one mentioned above) will assign value to the array index specified by position , which can itself go out of bounds of the array since you are not validating it. To compare the original value of i as-is against the value of position , you need to use the == comparison operation instead of the = assignment operator.

Try something more like this instead:

Sequence::Sequence(size_type sz)
{
    numElts = sz;
    elts = new int[sz];
    // or: elts = new int[sz]();

    for (size_type i = 0; i < sz; ++i) {
        elts[i] = 0;
    }
    // or: std::fill(elts, elts + sz, 0);
    // or: std::fill_n(elts, sz, 0);
}

void Sequence::insert(size_type position, value_type value)
{
    for (size_type i = 0; i < numElts; ++i) {
        if (i == position) {
            elts[i] = value;
            break;
        }
    }
}

However, insert() can be greatly simplified by removing the loop altogether:

void Sequence::insert(size_type position, value_type value)
{
    // NOTE: checking for '>= 0' can be skipped if size_type is unsigned ...
    if ((position >= 0) && (position < numElts)) {
        elts[position] = value;
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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