SFML community forums

Help => Graphics => Topic started by: kirilzh on April 24, 2018, 02:30:13 am

Title: Add vertices to a vector
Post by: kirilzh on April 24, 2018, 02:30:13 am
I'm trying to draw about a hundred lines on the screen. So far I'm testing my code with adding only two vertices to a vector but the code won't compile because of

error: expected primary-expression before ']' token vertices.push_back(line[]);

I'm not really experienced in C++ and any help would be greatly appreciated.

Here's my code so far.

    std::vector <sf::Vertex> vertices;

    sf::Vertex line[] =
    {
        sf::Vertex(sf::Vector2f(1, 0), sf::Color::Red),
        sf::Vertex(sf::Vector2f(1, 150), sf::Color::Green),
    };

    sf::Vertex line2[] =
    {
        sf::Vertex(sf::Vector2f(3, 0), sf::Color::Red),
        sf::Vertex(sf::Vector2f(3, 150), sf::Color::Green)
    };

    vertices.push_back(line[]);
    vertices.push_back(line2[]);
Title: Re: Add vertices to a vector
Post by: Geheim on April 24, 2018, 04:29:26 am
A good place to start would be the tutorials. (https://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php)
You should also read a good book (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for improving your C++ knowledge.
Title: Re: Add vertices to a vector
Post by: Laurent on April 24, 2018, 07:56:32 am
Yes, that's a basic C++ question not related to SFML, so you'd better improve your C++ skills first (by reading some good documentation, preferably a book) :)

Don't make things more complicated than they should be, just add the vertices directly to the vector:
vertices.push_back(sf::Vertex(sf::Vector2f(1, 0), sf::Color::Red));
vertices.push_back(sf::Vertex(sf::Vector2f(1, 150), sf::Color::Green));
vertices.push_back(sf::Vertex(sf::Vector2f(3, 0), sf::Color::Red));
vertices.push_back(sf::Vertex(sf::Vector2f(3, 150), sf::Color::Green));