Balázs Édes
Balázs Édes

Reputation: 13807

Vector push_back happens randomly

I have a pretty weird error while trying to implement a board game in QT. This is my second time, having the same issue, and now i think it's time to ask here.

I'm still at the game's logic part, which have a class named Field, and a class named Board. The Board class has a QVector < Field* > fields attribute, which is a vector of Field pointers. And here is the problem. I have a function which supposed to upload this vector with new Fields. Here is my source, it's pretty straight forward:

void Board::addFields()
{
    for(int i = 0; i<size; i++) //the board's size is size x size
    {
        for(int j = 0; j<size; j++)
        {
            Field * f = new Field();
            fields.push_back(f);
            //qDebug()<<i*size+j<<" "<<f;
        }
    }
    //qDebug()<<fields.size();
}

And after i got a ton of weird errors, i decided to write to the console the actual number of elements, the reference of the actual element, and after the two loop the size of the vector.

The result was pretty strange, i got no errors, but somtimes it added all my elements, sometimes it stopped at a random number of elements, and the size of the vector doesnt even got printed out like this:

result 1 (normal):

0 0xa173a8

...

24 0x701c18

25

result 2 (??? every time with a different number of elements):

0 0xa173a8

...

12 0xa17548

//and no vector size, i assume the function got a return; because of something

I tried it with both QVector, and std::vector, it's the same. Please if someone got any idea, what the problem could be, tell me!

Thanks, and sorry for my english!

EDIT

All my code is here:

field.h: http://paste2.org/p/1937231

field.cpp: http://paste2.org/p/1937232

player.h: http://paste2.org/p/1937238

player.cpp: http://paste2.org/p/1937239

board.h: http://paste2.org/p/1937234

board.cpp: http://paste2.org/p/1937235

main.cpp: http://paste2.org/p/1937243

Upvotes: 0

Views: 382

Answers (4)

Bal&#225;zs &#201;des
Bal&#225;zs &#201;des

Reputation: 13807

Thanks Everyone for the help, i found the solution.

Since i was making the logic part of a window application, i started a QT gui project. It generated for me a main() function with this:

QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();

I didn't know what this is, but because i didn't need any windows for now, i removed the code, and replaced it with my code, and a simple return 0; at the end. This must have caused the problem, because when i added the QApplication to the code again:

QApplication a(argc, argv);
Board b(10);
return a.exec();

It worked like a charm...

I don't know what to do in this case, if i could, i would give the point to all of you, who tried to help. But anyways, thank you very much for the help! :)

Upvotes: 1

UmNyobe
UmNyobe

Reputation: 22890

I don't know details of object initialization in C++, but

Board::Board(int size):size(size)
{
   playerOne = new Player("Player One");
   playerTwo = new Player("Player Two");
   newGame(this->size);
}

may be the cause of the trouble. indeed, newGame use fields which is not explicitely initialized. So try using

 Board::Board(int size):size(size), fields(size*size) //or just fields()
 {
  ...
  }

And tell us what happens.

Upvotes: 0

andrea.marangoni
andrea.marangoni

Reputation: 1499

Is size a member? for my experience if things happen randomly it's 98% caused by members not initialized..

Upvotes: 0

Constantinius
Constantinius

Reputation: 35059

When you type new Field(); you make a dynamic memory allocation. The returned pointer actually is quite random, and you should not rely on it to be sequential. But normally that is no problem.

And after i got a ton of weird errors, i decided to write to the console the actual number of elements, the reference of the actual element, and after the two loop the size of the vector.

What errors are you experiencing exactly?

Upvotes: 0

Related Questions