1

I have these two pieces of code that are messing up without throwing any errors:

The first piece is from a custom class which I am trying to push into an array.

class idRect {
    public:
        sf::FloatRect rect;
        int id;

        idRect(int _id, sf::FloatRect _rect) : id(_id), rect(_rect) {}
};

The second piece is where the function gets called.

if((deltaX + deltaY) < 500) { //Taxi distance calculation
    cout << endl << "Passed check" << endl;
    gloAreas.push_back(idRect(id, entity.getGlobalBounds()));
}

gloAreas is a globally defined vector which contains idRect objects.

As said earlier I have observed from the console that "Passed check" outputs and that the size of my vector doesn't increase EDIT: globally.

Edit: The error also seems rather random and only happens for 1 in 6 instances of the objects calling the push_back functions.

I'm using SFML for the sf::FloatRect which is basically just a vector of 4 floats. getGlobalBounds() is another function from SFML that returns the bounding rectangle of a sprite in sf::FloatRect format.

Any ideas of what is going wrong?

Sincerely, BarrensZeppelin

EDIT 2: The error seems to have erupted due to a mix between my own incompetence and std::multiset's sorting, maybe I'll come back for that in another thread ^^ (With a sscce ofc) Thank you guys for you time and help.

4

2 回答 2

3

如果gloAreas定义为static,则它不是真正的全局变量。它将具有全局范围,但将为每个翻译单元创建一个副本。

对于全局,您需要extern在单个实现文件中声明并定义它。

免责声明:答案只是猜测,我的水晶球今天可能会关闭......

于 2012-11-27T20:49:37.207 回答
1

我的水晶球回答:您已经gloAreas在内部范围内重新定义,如下所示:

vector<idRect> gloAreas; // defines global

void F( vector<idRect> gloAreas ) // defines local instance
{
  gloAreas.push_back(); // affects local instance
  return;               // destroys local instance 
}
int main() {
  F(gloAreas); // Copies global instance to parameter
               // global remains unchanged.
}
于 2012-11-27T21:04:03.623 回答