Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
edited tags
Link
songyuanyao
  • 173.5k
  • 16
  • 327
  • 432
Remove fluff and tags
Source Link
Ken Y-N
  • 15.2k
  • 22
  • 80
  • 122

I'm experiencing some "odd behaviour" with populating a vector value with the use of the constructor.

So, I have this block class that populates this static vector called blockList whenever the constructor is called.

static std::vector<Block*> blockList; Block::Block(float x, float y) : RectangleShape(sf::Vector2f(WIDTH, HEIGHT)) { blockList.push_back(this); setFillColor(sf::Color(221, 221, 221, 255)); setPosition(x, y); } 

Now, when I populate the values in another class I have called "Game" ; in its constructor like so:

Game::Game(sf::RenderWindow& window) { // other code here Block firstBlock(10, 10); // possible more blocks here } 

and then attempt to draw, it crashed. In addition, the coordinates of this block print out to be (x: 9.8439e-12, y: 9.84394e-12). Now what I've tried doing is throwing it in the draw method (which I'm aware is bad as it's constantly being called but for debugging purposes):

void Game::drawObjects() { // draw other shapes here Block oneBlock(50.0f, 50.0f); std::cout << std::string(10, '-') << std::endl; for (Block* block : Block::getBlockList()) { std::cout << block->getPosition().x << " - " << block->getPosition().y << std::endl; window->draw(*block); } std::cout << std::string(10, '-') << std::endl; } 

The coordinates and the drawing of the object work perfectly (even if the vector is being populated heavily). So I'm wondering why this is happening and a possible way to work around this. I don't like the idea, but perhaps I should have flag to detect whether or not they've been created and then create them in the draw method? Thanks in advance!

I'm experiencing some "odd behaviour" with populating a vector value with the use of the constructor.

So, I have this block class that populates this static vector called blockList whenever the constructor is called.

static std::vector<Block*> blockList; Block::Block(float x, float y) : RectangleShape(sf::Vector2f(WIDTH, HEIGHT)) { blockList.push_back(this); setFillColor(sf::Color(221, 221, 221, 255)); setPosition(x, y); } 

Now, when I populate the values in another class I have called "Game" ; in its constructor like so:

Game::Game(sf::RenderWindow& window) { // other code here Block firstBlock(10, 10); // possible more blocks here } 

and then attempt to draw, it crashed. In addition, the coordinates of this block print out to be (x: 9.8439e-12, y: 9.84394e-12). Now what I've tried doing is throwing it in the draw method (which I'm aware is bad as it's constantly being called but for debugging purposes):

void Game::drawObjects() { // draw other shapes here Block oneBlock(50.0f, 50.0f); std::cout << std::string(10, '-') << std::endl; for (Block* block : Block::getBlockList()) { std::cout << block->getPosition().x << " - " << block->getPosition().y << std::endl; window->draw(*block); } std::cout << std::string(10, '-') << std::endl; } 

The coordinates and the drawing of the object work perfectly (even if the vector is being populated heavily). So I'm wondering why this is happening and a possible way to work around this. I don't like the idea, but perhaps I should have flag to detect whether or not they've been created and then create them in the draw method? Thanks in advance!

I'm experiencing some "odd behaviour" with populating a vector value with the use of the constructor.

So, I have this block class that populates this static vector called blockList whenever the constructor is called.

static std::vector<Block*> blockList; Block::Block(float x, float y) : RectangleShape(sf::Vector2f(WIDTH, HEIGHT)) { blockList.push_back(this); setFillColor(sf::Color(221, 221, 221, 255)); setPosition(x, y); } 

Now, when I populate the values in another class I have called "Game" ; in its constructor like so:

Game::Game(sf::RenderWindow& window) { // other code here Block firstBlock(10, 10); // possible more blocks here } 

and then attempt to draw, it crashed. In addition, the coordinates of this block print out to be (x: 9.8439e-12, y: 9.84394e-12). Now what I've tried doing is throwing it in the draw method (which I'm aware is bad as it's constantly being called but for debugging purposes):

void Game::drawObjects() { // draw other shapes here Block oneBlock(50.0f, 50.0f); std::cout << std::string(10, '-') << std::endl; for (Block* block : Block::getBlockList()) { std::cout << block->getPosition().x << " - " << block->getPosition().y << std::endl; window->draw(*block); } std::cout << std::string(10, '-') << std::endl; } 

The coordinates and the drawing of the object work perfectly (even if the vector is being populated heavily). So I'm wondering why this is happening and a possible way to work around this. I don't like the idea, but perhaps I should have flag to detect whether or not they've been created and then create them in the draw method?

Source Link
Jujunol
  • 469
  • 6
  • 18

C++ Odd behaviour with vector populated with constructor

I'm experiencing some "odd behaviour" with populating a vector value with the use of the constructor.

So, I have this block class that populates this static vector called blockList whenever the constructor is called.

static std::vector<Block*> blockList; Block::Block(float x, float y) : RectangleShape(sf::Vector2f(WIDTH, HEIGHT)) { blockList.push_back(this); setFillColor(sf::Color(221, 221, 221, 255)); setPosition(x, y); } 

Now, when I populate the values in another class I have called "Game" ; in its constructor like so:

Game::Game(sf::RenderWindow& window) { // other code here Block firstBlock(10, 10); // possible more blocks here } 

and then attempt to draw, it crashed. In addition, the coordinates of this block print out to be (x: 9.8439e-12, y: 9.84394e-12). Now what I've tried doing is throwing it in the draw method (which I'm aware is bad as it's constantly being called but for debugging purposes):

void Game::drawObjects() { // draw other shapes here Block oneBlock(50.0f, 50.0f); std::cout << std::string(10, '-') << std::endl; for (Block* block : Block::getBlockList()) { std::cout << block->getPosition().x << " - " << block->getPosition().y << std::endl; window->draw(*block); } std::cout << std::string(10, '-') << std::endl; } 

The coordinates and the drawing of the object work perfectly (even if the vector is being populated heavily). So I'm wondering why this is happening and a possible way to work around this. I don't like the idea, but perhaps I should have flag to detect whether or not they've been created and then create them in the draw method? Thanks in advance!