Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [c++]Can't initialize bool array with for loop  (Read 2001 times)

0 Members and 1 Guest are viewing this topic.

Overkiller

  • Newbie
  • *
  • Posts: 8
    • View Profile
[c++]Can't initialize bool array with for loop
« on: August 13, 2013, 12:50:54 pm »
Hello!

Sorry for my lame question, but how to initialize bool array with any of loops? I was trying to initialize with for loop:
mouse class:
//there is declaration of my array, the rest is not improtant:
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
using sf::Mouse;
using sf::Vector2i;
using sf::Vector2f;
using sf::RectangleShape;
using sf::Color;
using sf::FloatRect;
using std::cout;
using std::endl;
class mysz
{
public:
        mysz(void);
        Vector2i pozycjaMyszy;
        Vector2i pobierzPozycjeMyszy();
        Vector2f pozycjaFloat;
        RectangleShape kwadratMysz;
        FloatRect kolizjaKwadratMysz;
        bool kolizja[9]; // this is it
        void ustawParametryKwadratu();
        void ustawPozycje();
        void sprawdzKolizje(FloatRect porownywana, int krokPetli);
        ~mysz(void);

};
mouse cpp:
#include "mysz.h"


mysz::mysz(void)
{
        for(int i = 0; i > 9; i++)
        {
                kolizja[i] = false; // here i try to initialize the array
        }
}

Vector2i mysz::pobierzPozycjeMyszy()
{
        pozycjaMyszy = Mouse::getPosition();
        pozycjaFloat = static_cast<Vector2f>(pozycjaMyszy);
        return pozycjaMyszy;
}
void mysz::ustawParametryKwadratu()
{
        kwadratMysz.setSize(Vector2f (32,32));
        kwadratMysz.setFillColor(Color::White);
        kolizjaKwadratMysz = kwadratMysz.getGlobalBounds();
}
void mysz::sprawdzKolizje(FloatRect porownywana, int krokPetli)
{

        if(kolizjaKwadratMysz.intersects(porownywana))
        {
//              wystapilyKolizje[krokPetli] = true;
        }
}
mysz::~mysz(void)
{
}

 
but VS 2012 debugger show that all of array fields have: "true (204)"
So, can someone help with my problem?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [c++]Can't initialize bool array with for loop
« Reply #1 on: August 13, 2013, 12:54:14 pm »
Quote
for(int i = 0; i > 9; i++)
> should be < ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [c++]Can't initialize bool array with for loop
« Reply #2 on: August 13, 2013, 01:08:07 pm »
Don't write loops if there are STL algorithms for this task:
std::fill(kolizia, kolizia+9, false);

Or use std::array, which is a good idea anyway:
std::array<bool, 9> kolizja;
...
kolizja.fill(false);

Or initialize the array directly:
mysz::mysz(void)
: kolizja() // sets all elements to false
{
}

By the way, you should avoid using in header files. Why don't you explicitly write sf::Vector2i? That's the purpose of namespaces ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Overkiller

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: [c++]Can't initialize bool array with for loop
« Reply #3 on: August 13, 2013, 01:49:57 pm »
@Laurent
OMG, yes that's it! :D I was an oversight...

@Nexus
I didn't knew about std::fill and std::array.
Why i should avoid using in header files? I was always writing using namespace sf or using sf::xxx

So far thank you for help :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [c++]Can't initialize bool array with for loop
« Reply #4 on: August 13, 2013, 02:02:49 pm »
There is a reason why namespaces exist -- other than to remove the namespace prefix ;)

When you always write using or using namespace, you are defeating the purpose of namespaces. Identifiers then all look the same, independently of the library. This makes it more difficult to distinguish between different libraries in the code, and it can introduce name collisions if two identifiers from different libraries are called the same.

In headers, this is especially a problem since you put all the identifiers into global namespace, for all files that include the header. This cannot be undone in .cpp files. In case the header is part of a library, it's even worse.

See also a question on Stack Overflow.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything