G
G
Grigory Dikiy2015-10-10 17:42:52
C++ / C#
Grigory Dikiy, 2015-10-10 17:42:52

Doesn't work inserted into a vector?

Good day! I'm doing term paper for uni. My words are a vector. But here's what's interesting when adding a new word to the vector of words, it crashes, and if 2 - 3 inserts work fine, but then it just crashes.

void Line::CreateNewWord( string character )
{
    int i;

    // Определение позиции итератора
    for ( i = 0, it = words.begin(); it < words.end(); it++, i++)
        if( i == activeWord )
            break;

    // Если не первое слово
    if( width )
        activeWord++;

    // Создание нового слова
    word = new Word();
    word->Insert( character );
    words.insert( it, *word );

    width += FONT_WIDTH;
}

What could be causing this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2015-10-10
@frilix

it<words.end();

Try to compare iterators with help == and !=. More/less comparison does not work with all iterators, you should not get used to it.
Is the copy constructor correctly defined? Even if so, why such perversions with creating an object first on the heap, and then copying it by value? Isn't it easier to work with Word on a stack, or vice versa, save a pointer to a vector? By the way, you also forget to delete the Word class object created on the heap.

K
Konstantin, 2015-10-10
@Drakonn

oh, these lovers of global variables, their paws would be torn off .. and so - debug and look when you go beyond the vector, string or something else, and for the future - give up global variables and do more checks in methods in order to get rid of crashes and show the reasons why. By the way, catch it with a debugger, since you know that it is on 2-3 that the addition of the program breaks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question