SFML community forums

Help => Graphics => Topic started by: arkan01 on September 14, 2016, 04:48:41 pm

Title: String error on a constructor of a class
Post by: arkan01 on September 14, 2016, 04:48:41 pm
I recently started to learn c++, so i'd like to have detailed explanations if you can. :)
(If i posted in the wrong section, i'm sorry)

Here's the code:

Player.h:
class Player {
public:
        Player(std::string, sf::Color);
        void Update();
        void Draw(sf::RenderWindow*, sf::RenderStates);

private:
        ...
        sf::Text nickname;
        sf::Font font;
}

Player.cpp
Player::Player(std::string _nickname, sf::Color color) {
...
        nickname.setFont(font);
        nickname.setString(_nickname);
        nickname.setCharacterSize(12);
        nickname.setFillColor(sf::Color(255, 255, 255));
}

void Player::Draw(sf::RenderWindow* window, sf::RenderStates states) {
        window->draw(_player, states);
        window->draw(nickname, states);
}

Here's the problem:

Main.cpp:
int main() {
        ...

        std::vector<Player> players;

        while (window.isOpen()) {

                std::string nickname; std::cin >> nickname;
                players.push_back(Player::Player(nickname, sf::Color(102, 0, 0)));

                window.clear();
       
                window.draw(cursor, states);
                for (auto player : players)
                        player.Draw(&window, states);
                window.display();
         }
}

When i try to write the nickname on the console, and press ente it gives me the following error:
"Debug assertion failed! Expression: map/set insert iterator out of range".

Thanks in advance.
Title: AW: String error on a constructor of a class
Post by: eXpl0it3r on September 14, 2016, 06:06:10 pm
For more general C++ questions it's better to use sites like StackOverflow.
Also generally when you get an assertion or crash you should run it in debug mode and figure out where the issue originates from. Learning to use a debugger is an important skill for a programmer.
Additionally you can track down bugs by isolating them more and more. Either by removing code that isn't needed or by starting from scratch and only adding things until you can reproduce the bug again
Title: Re: String error on a constructor of a class
Post by: arkan01 on September 14, 2016, 06:12:41 pm
Thank you for the fast reply.

Following your instruction, i runned it on debug mode, and now i understand the problem.
Again thank you for this explanation. :)