4

I'm trying to compile a *.cpp file:

#include <SFML/Graphics.hpp>

using namespace sf;

int main()
{
    
    RenderWindow window(VideoMode(400, 400), L"New project", Style::Default);
    
    window.setVerticalSyncEnabled(true);
       
    CircleShape shape(100.f,3); 
    shape.setPosition(100, 100);           
    shape.setFillColor(Color::Magenta);
        
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }
        
        window.clear(Color::Blue);
        window.draw(shape);
        window.display();
    }
    return 0;
}

I put g++ Durak.cpp -o output.exe -I"C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include" -L"C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\lib" -lsfml-graphics -lsfml-window -lsfml-system in the console, but get errors:

main.cpp: In function 'int main()':
main.cpp:8:43: error: no matching function for call to 'sf::VideoMode::VideoMode(int, int)'
    8 |     RenderWindow window(VideoMode(400, 400), L"New project", Style::Default);
      |                                           ^
In file included from C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Graphics/RenderWindow.hpp:35,
                 from C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Graphics.hpp:45,
                 from main.cpp:1:
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/VideoMode.hpp:61:14: note: candidate: 'sf::VideoMode::VideoMode(sf::Vector2u, unsigned int)'
   61 |     explicit VideoMode(Vector2u modeSize, unsigned int modeBitsPerPixel = 32);
      |              ^~~~~~~~~
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/VideoMode.hpp:61:33: note:   no known conversion for argument 1 from 'int' to 'sf::Vector2u' {aka 'sf::Vector2<unsigned int>'}
   61 |     explicit VideoMode(Vector2u modeSize, unsigned int modeBitsPerPixel = 32);
      |                        ~~~~~~~~~^~~~~~~~
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/VideoMode.hpp:52:5: note: candidate: 'constexpr sf::VideoMode::VideoMode()'
   52 |     VideoMode() = default;
      |     ^~~~~~~~~
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/VideoMode.hpp:52:5: note:   candidate expects 0 arguments, 2 provided
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/VideoMode.hpp:43:23: note: candidate: 'constexpr sf::VideoMode::VideoMode(const sf::VideoMode&)'
   43 | class SFML_WINDOW_API VideoMode
      |                       ^~~~~~~~~
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/VideoMode.hpp:43:23: note:   candidate expects 1 argument, 2 provided
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/VideoMode.hpp:43:23: note: candidate: 'constexpr sf::VideoMode::VideoMode(sf::VideoMode&&)'
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/VideoMode.hpp:43:23: note:   candidate expects 1 argument, 2 provided
main.cpp:13:22: error: no matching function for call to 'sf::CircleShape::setPosition(int, int)'
   13 |     shape.setPosition(100, 100);
      |     ~~~~~~~~~~~~~~~~~^~~~~~~~~~
In file included from C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Graphics/Shape.hpp:37,
                 from C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Graphics/CircleShape.hpp:32,
                 from C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Graphics.hpp:32:
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Graphics/Transformable.hpp:70:10: note: candidate: 'void sf::Transformable::setPosition(sf::Vector2f)'
   70 |     void setPosition(Vector2f position);
      |          ^~~~~~~~~~~
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Graphics/Transformable.hpp:70:10: note:   candidate expects 1 argument, 2 provided
main.cpp:18:15: error: no matching function for call to 'sf::Event::Event()'
   18 |         Event event;
      |               ^~~~~
In file included from C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/WindowBase.inl:28,
                 from C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/WindowBase.hpp:617,
                 from C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/Window.hpp:32,
                 from C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Graphics/RenderWindow.hpp:36:
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/Event.hpp:306:5: note: candidate: 'template<class TEventSubtype> sf::Event::Event(const TEventSubtype&)'
  306 |     Event(const TEventSubtype& eventSubtype);
      |     ^~~~~
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/Event.hpp:306:5: note:   template argument deduction/substitution failed:
main.cpp:18:15: note:   candidate expects 1 argument, 0 provided
   18 |         Event event;
      |               ^~~~~
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/Event.hpp:46:7: note: candidate: 'constexpr sf::Event::Event(const sf::Event&)'
   46 | class Event
      |       ^~~~~
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/Event.hpp:46:7: note:   candidate expects 1 argument, 0 provided
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/Event.hpp:46:7: note: candidate: 'constexpr sf::Event::Event(sf::Event&&)'
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/Event.hpp:46:7: note:   candidate expects 1 argument, 0 provided
main.cpp:19:32: error: no matching function for call to 'sf::RenderWindow::pollEvent(sf::Event&)'
   19 |         while (window.pollEvent(event))
      |                ~~~~~~~~~~~~~~~~^~~~~~~
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/WindowBase.hpp:226:40: note: candidate: 'std::optional<sf::Event> sf::WindowBase::pollEvent()'
  226 |     [[nodiscard]] std::optional<Event> pollEvent();
      |                                        ^~~~~~~~~
C:\Users\dalvo\IT\SFML (MinGW)\SFML-3.0.0\include/SFML/Window/WindowBase.hpp:226:40: note:   candidate expects 0 arguments, 1 provided
main.cpp:21:23: error: 'class sf::Event' has no member named 'type'
   21 |             if (event.type == Event::Closed)
      |                       ^~~~
main.cpp:21:44: error: expected primary-expression before ')' token
   21 |             if (event.type == Event::Closed)
      |                                            ^

My versions are as follows: g++ (Rev3, Built by MSYS2 project) 13.2.0; SFML-3.0.0. The line of a command promt's text I asked from ChatGpt. The code I copy-pasted from a website with a guide: https://habr.com/ru/articles/703500/. Others use this code successfully but I don't. Please help me, I've been working on this problem for a several days (want to quit programming already...)

6
  • 5
    You're reading a tutorial for SFML 2.6, while attempting to use SFML 3.0, which is not backwards compatible. Either download the correct version of the library, or find a tutorial for 3.0. Commented Jan 13 at 16:52
  • 2
    candidate: 'sf::VideoMode::VideoMode(sf::Vector2u, unsigned int)'. Try VideoMode({400, 400}). Commented Jan 13 at 16:55
  • 1
    Don't quit but honestly learn programming with something easier than SFML. The best way to learn programming is to read about the language and then attempt to write your own code (or do the exercises). Copying code you don't understand from a website does not teach you very much. Commented Jan 13 at 16:55
  • I switched to SFML-2.6.2, but now I get undefined reference to `__imp__ZNK2sf10WindowBase6isOpenEv' collect2.exe: error: ld returned 1 exit status. There're much more "undefined references", but I left only 1 just for example. (Tried compiling both VideoMode({400, 400}) and VideoMode(400, 400). The same errors) Commented Jan 13 at 17:12
  • I recommend sticking with SFML3 if you are making a new project. Just look at the errors you get and look-up the SFML3 documentation, like the one for sf::Event which shows how the loop is done in SFML3. The demangled name for the function you mentioned is sf::WindowBase::isOpen() const. Perhaps you didn't link with the library containing that. Commented Jan 13 at 17:15

1 Answer 1

5

Since you are using SFML3, you need to make some changes. Most functions that previously had overloads for taking height and width separately now have a constructor or member function taking a sf::Vector2u, sf::Vector2f or similar, like void CircleShape::setPosition (Vector2f position)

There's also a Migration to SFML3 guide. Just enter VideoMode in the search box and it will show you how to make the call properly in SFML3.

Your program with the issues fixed:

#include <SFML/Graphics.hpp>

using namespace sf;

int main() {
    RenderWindow window(VideoMode({400, 400}), L"New project", Style::Default);
    //                            ^        ^

    window.setVerticalSyncEnabled(true);

    CircleShape shape(100.f, 3);
    // getGeometricCenter() is new in SFML3. 
    // Set origin in the middle of the shape:
    shape.setOrigin(shape.getGeometricCenter());
    shape.setPosition({200, 200});               // center of window
    //                ^        ^
    shape.setFillColor(Color::Magenta);

    while(window.isOpen()) {
        while(auto event = window.pollEvent()) { // polling in SFML3
            if(event->is<sf::Event::Closed>()) {
                window.close();
            } else if(auto keyPressed = event->getIf<sf::Event::KeyPressed>()) {
                if(keyPressed->scancode == sf::Keyboard::Scancode::Escape)
                    window.close();
            }
        }

        window.clear(Color::Blue);
        window.draw(shape);
        window.display();
        shape.rotate(degrees(1.0f)); // rotate takes an sf::Angle in SFML3
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.