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

Author Topic: String error on a constructor of a class  (Read 1406 times)

0 Members and 1 Guest are viewing this topic.

arkan01

  • Newbie
  • *
  • Posts: 2
    • View Profile
String error on a constructor of a class
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: String error on a constructor of a class
« Reply #1 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
« Last Edit: September 14, 2016, 08:03:55 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

arkan01

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: String error on a constructor of a class
« Reply #2 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. :)

 

anything