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

Author Topic: Small program wont compile [Linux]  (Read 2038 times)

0 Members and 1 Guest are viewing this topic.

lewashby

  • Newbie
  • *
  • Posts: 7
    • View Profile
Small program wont compile [Linux]
« on: July 24, 2016, 04:00:06 am »


    #include <SFML/Window.hpp>
   
    int main()
    {
        sf::Window window(sf::VideoMode(200, 150), "My Window");
   
        while(window.isOpen())
        {
            sf::Event event;
   
            while(window.pollEvent(event))
            {
                switch(event.type)
                case sf::Event::EventType::Closed:
                    window.close();
                    break;
                case sf::Event::EventType::KeyPressed:
                    if(event.key.code == sf::keyboard::Space)
                    {
                        window.setTitle("Space Pressed");
                    }
                    break;
            }
        }
   
        return 0;
    }

When I try to compile the program above with this compile line I get the errors listed below.
g++ -o run2 main.cpp -lsfml-window -lsfml-graphics -lsfml-system



    main.cpp: In function ‘int main()’:
    main.cpp:14:29: error: ‘sf::Event::EventType’ is not a class or namespace
                 case sf::Event::EventType::Closed:
                                 ^
    main.cpp:13:19: warning: enumeration value ‘Closed’ not handled in switch [-Wswitch]
                 switch(event.type)
                       ^
    main.cpp:17:29: error: ‘sf::Event::EventType’ is not a class or namespace
                 case sf::Event::EventType::KeyPressed:
                                 ^
    main.cpp:17:13: error: case label ‘<expression error>’ not within a switch statement
                 case sf::Event::EventType::KeyPressed:
                 ^
    main.cpp:18:42: error: ‘sf::keyboard’ has not been declared
                     if(event.key.code == sf::keyboard::Space)
                                              ^
   

But odly enough if I cut the program down a little it compiles just fine. The program below compile without a problem.



    #include <SFML/Window.hpp>
   
    int main()
    {
        sf::Window window(sf::VideoMode(200, 150), "My Window");
   
        while(window.isOpen())
        {
            sf::Event event;
   
            while(window.pollEvent(event))
            {
                if(event.type == sf::Event::Closed)
                {
                    window.close();
                }
            }
        }
   
        return 0;
    }


My sfml file are stored under /usr/local/include/SFML/



    garrett@mint-desktop SFML $ pwd
    /usr/local/include/SFML
    garrett@mint-desktop SFML $ ls -lR
    .:
    total 60
    drwxr-xr-x 2 garrett garrett 4096 Oct 27  2015 Audio
    -rwxr-xr-x 1 garrett garrett 2095 May  8  2015 Audio.hpp
    -rwxr-xr-x 1 garrett garrett 5342 Sep 11  2015 Config.hpp
    drwxr-xr-x 2 garrett garrett 4096 Oct 27  2015 Graphics
    -rwxr-xr-x 1 garrett garrett 2530 May  8  2015 Graphics.hpp
    -rwxr-xr-x 1 garrett garrett 1521 May  8  2015 Main.hpp
    drwxr-xr-x 2 garrett garrett 4096 Oct 27  2015 Network
    -rwxr-xr-x 1 garrett garrett 1982 May  8  2015 Network.hpp
    -rwxr-xr-x 1 garrett garrett 2252 May  8  2015 OpenGL.hpp
    drwxr-xr-x 2 garrett garrett 4096 Jul 23 20:02 SFML
    drwxr-xr-x 2 garrett garrett 4096 Oct 27  2015 System
    -rwxr-xr-x 1 garrett garrett 2287 May  8  2015 System.hpp
    drwxr-xr-x 2 garrett garrett 4096 Oct 27  2015 Window
    -rwxr-xr-x 1 garrett garrett 2046 May  8  2015 Window.hpp
   
    ./Audio:
    total 160
    -rw-r--r-- 1 garrett garrett  2327 May  8  2015 AlResource.hpp
    -rw-r--r-- 1 garrett garrett  1662 May  8  2015 Export.hpp
    -rw-r--r-- 1 garrett garrett  9318 May  8  2015 InputSoundFile.hpp
    -rw-r--r-- 1 garrett garrett  8910 May  8  2015 Listener.hpp
    -rw-r--r-- 1 garrett garrett  8232 Jul  6  2015 Music.hpp
    -rw-r--r-- 1 garrett garrett  4523 May  8  2015 OutputSoundFile.hpp
    -rw-r--r-- 1 garrett garrett 12929 May  8  2015 SoundBuffer.hpp
    -rw-r--r-- 1 garrett garrett  4848 May  8  2015 SoundBufferRecorder.hpp
    -rw-r--r-- 1 garrett garrett  7190 May  8  2015 SoundFileFactory.hpp
    -rw-r--r-- 1 garrett garrett  3154 May  8  2015 SoundFileFactory.inl
    -rw-r--r-- 1 garrett garrett  5889 May  8  2015 SoundFileReader.hpp
    -rw-r--r-- 1 garrett garrett  4430 May  8  2015 SoundFileWriter.hpp
    -rw-r--r-- 1 garrett garrett  9266 May  8  2015 Sound.hpp
    -rw-r--r-- 1 garrett garrett 14059 Jul  6  2015 SoundRecorder.hpp
    -rw-r--r-- 1 garrett garrett 10387 May  8  2015 SoundSource.hpp
    -rw-r--r-- 1 garrett garrett 14785 May  8  2015 SoundStream.hpp
   
    ./Graphics:
    total 332
    -rw-r--r-- 1 garrett garrett  9700 May  8  2015 BlendMode.hpp
    -rw-r--r-- 1 garrett garrett  5344 May  8  2015 CircleShape.hpp
    -rw-r--r-- 1 garrett garrett  9789 May  8  2015 Color.hpp
    -rw-r--r-- 1 garrett garrett  5391 May  8  2015 ConvexShape.hpp
    -rw-r--r-- 1 garrett garrett  3822 May  8  2015 Drawable.hpp
    -rw-r--r-- 1 garrett garrett  1680 May  8  2015 Export.hpp
    -rw-r--r-- 1 garrett garrett 17082 Sep 11  2015 Font.hpp
    -rw-r--r-- 1 garrett garrett  2753 May  8  2015 Glyph.hpp
    -rw-r--r-- 1 garrett garrett 12310 May  8  2015 Image.hpp
    -rw-r--r-- 1 garrett garrett  2194 May  8  2015 PrimitiveType.hpp
    -rw-r--r-- 1 garrett garrett  4465 May  8  2015 RectangleShape.hpp
    -rw-r--r-- 1 garrett garrett  8763 May  8  2015 Rect.hpp
    -rw-r--r-- 1 garrett garrett  5100 May  8  2015 Rect.inl
    -rw-r--r-- 1 garrett garrett  6538 Jul  6  2015 RenderStates.hpp
    -rw-r--r-- 1 garrett garrett 17532 May  8  2015 RenderTarget.hpp
    -rw-r--r-- 1 garrett garrett 10188 May  8  2015 RenderTexture.hpp
    -rw-r--r-- 1 garrett garrett  9637 May  8  2015 RenderWindow.hpp
    -rw-r--r-- 1 garrett garrett 26008 Jul  6  2015 Shader.hpp
    -rw-r--r-- 1 garrett garrett 13668 May  8  2015 Shape.hpp
    -rw-r--r-- 1 garrett garrett 10685 May  8  2015 Sprite.hpp
    -rw-r--r-- 1 garrett garrett 13917 May  8  2015 Text.hpp
    -rw-r--r-- 1 garrett garrett 24256 Sep 11  2015 Texture.hpp
    -rw-r--r-- 1 garrett garrett 16396 May  8  2015 Transformable.hpp
    -rw-r--r-- 1 garrett garrett 16047 May  8  2015 Transform.hpp
    -rw-r--r-- 1 garrett garrett  7768 May  8  2015 VertexArray.hpp
    -rw-r--r-- 1 garrett garrett  5615 May  8  2015 Vertex.hpp
    -rw-r--r-- 1 garrett garrett 12285 May  8  2015 View.hpp
   
    ./Network:
    total 140
    -rw-r--r-- 1 garrett garrett  1674 May  8  2015 Export.hpp
    -rw-r--r-- 1 garrett garrett 24912 May  8  2015 Ftp.hpp
    -rw-r--r-- 1 garrett garrett 19862 May  8  2015 Http.hpp
    -rw-r--r-- 1 garrett garrett 12663 May  8  2015 IpAddress.hpp
    -rw-r--r-- 1 garrett garrett 13974 May  8  2015 Packet.hpp
    -rw-r--r-- 1 garrett garrett  1773 May  8  2015 SocketHandle.hpp
    -rw-r--r-- 1 garrett garrett  7701 May  8  2015 Socket.hpp
    -rw-r--r-- 1 garrett garrett  9312 May  8  2015 SocketSelector.hpp
    -rw-r--r-- 1 garrett garrett  5366 May  8  2015 TcpListener.hpp
    -rw-r--r-- 1 garrett garrett 11300 May  8  2015 TcpSocket.hpp
    -rw-r--r-- 1 garrett garrett 10735 May  8  2015 UdpSocket.hpp
   
    ./SFML:
    total 0
   
    ./System:
    total 244
    -rw-r--r-- 1 garrett garrett  3713 May  8  2015 Clock.hpp
    -rw-r--r-- 1 garrett garrett  2641 May  8  2015 Err.hpp
    -rw-r--r-- 1 garrett garrett  1668 May  8  2015 Export.hpp
    -rw-r--r-- 1 garrett garrett  5442 Jul  6  2015 FileInputStream.hpp
    -rw-r--r-- 1 garrett garrett  4855 May  8  2015 InputStream.hpp
    -rw-r--r-- 1 garrett garrett  4246 May  8  2015 Lock.hpp
    -rw-r--r-- 1 garrett garrett  5193 May  8  2015 MemoryInputStream.hpp
    -rw-r--r-- 1 garrett garrett  4972 May  8  2015 Mutex.hpp
    -rw-r--r-- 1 garrett garrett  4302 May  8  2015 NonCopyable.hpp
    -rw-r--r-- 1 garrett garrett  1821 May  8  2015 Sleep.hpp
    -rw-r--r-- 1 garrett garrett 25366 May  8  2015 String.hpp
    -rw-r--r-- 1 garrett garrett  1785 May  8  2015 String.inl
    -rw-r--r-- 1 garrett garrett  9233 May  8  2015 Thread.hpp
    -rw-r--r-- 1 garrett garrett  2776 May  8  2015 Thread.inl
    -rw-r--r-- 1 garrett garrett  3415 May  8  2015 ThreadLocal.hpp
    -rw-r--r-- 1 garrett garrett  5000 May  8  2015 ThreadLocalPtr.hpp
    -rw-r--r-- 1 garrett garrett  2210 May  8  2015 ThreadLocalPtr.inl
    -rw-r--r-- 1 garrett garrett 16061 May  8  2015 Time.hpp
    -rw-r--r-- 1 garrett garrett 35528 May  8  2015 Utf.hpp
    -rw-r--r-- 1 garrett garrett 21501 May  8  2015 Utf.inl
    -rw-r--r-- 1 garrett garrett  9778 May  8  2015 Vector2.hpp
    -rw-r--r-- 1 garrett garrett  4158 May  8  2015 Vector2.inl
    -rw-r--r-- 1 garrett garrett  9785 May  8  2015 Vector3.hpp
    -rw-r--r-- 1 garrett garrett  4428 May  8  2015 Vector3.inl
   
    ./Window:
    total 132
    -rw-r--r-- 1 garrett garrett  4982 May  8  2015 Context.hpp
    -rw-r--r-- 1 garrett garrett  6293 May  8  2015 ContextSettings.hpp
    -rw-r--r-- 1 garrett garrett 13214 May  8  2015 Event.hpp
    -rw-r--r-- 1 garrett garrett  1668 May  8  2015 Export.hpp
    -rw-r--r-- 1 garrett garrett  2586 May  8  2015 GlResource.hpp
    -rw-r--r-- 1 garrett garrett  8608 Jul  6  2015 Joystick.hpp
    -rw-r--r-- 1 garrett garrett  8219 May  8  2015 Keyboard.hpp
    -rw-r--r-- 1 garrett garrett  6321 May  8  2015 Mouse.hpp
    -rw-r--r-- 1 garrett garrett  5820 May  8  2015 Sensor.hpp
    -rw-r--r-- 1 garrett garrett  5037 May  8  2015 Touch.hpp
    -rw-r--r-- 1 garrett garrett  8690 May  8  2015 VideoMode.hpp
    -rw-r--r-- 1 garrett garrett  2368 May  8  2015 WindowHandle.hpp
    -rw-r--r-- 1 garrett garrett 23120 May  8  2015 Window.hpp
    -rw-r--r-- 1 garrett garrett  1961 May  8  2015 WindowStyle.hpp
    garrett@mint-desktop SFML $
   

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Small program wont compile [Linux]
« Reply #1 on: July 24, 2016, 07:12:06 am »
It's not really odd. In the first code you use the name of the enum in the case statements, which is not how regular enums should be used. While in the seconde code you leave it out.
It should always be sf::Event::KeyPressed, sf::Event::Closed etc
See the tutorial on events. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lewashby

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: AW: Small program wont compile [Linux]
« Reply #2 on: July 24, 2016, 10:03:24 pm »
It's not really odd. In the first code you use the name of the enum in the case statements, which is not how regular enums should be used. While in the seconde code you leave it out.
It should always be sf::Event::KeyPressed, sf::Event::Closed etc
See the tutorial on events. ;)

I revised and took out the EventType as you stated and now I'm getting this error.

main.cpp: In function ‘int main()’:
main.cpp:17:13: error: case label ‘KeyPressed’ not within a switch statement
             case sf::Event::KeyPressed:

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Small program wont compile [Linux]
« Reply #3 on: July 24, 2016, 10:27:10 pm »
Your switch statement has no brackets ("{...}"). They're not optional for switch().

lewashby

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Small program wont compile [Linux]
« Reply #4 on: July 24, 2016, 10:48:10 pm »
Your switch statement has no brackets ("{...}"). They're not optional for switch().

Thanks Mario, that got it. I'm not the greatest in programming anyway but even what I do I rarely use switch statements. I'm following along a video on udemy.com and watched the guy compile that code wihout a problem, he was using Visual Studio and I'm using VIM and g++, I don't know if that would make a difference or not.

lewashby

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Small program wont compile [Linux]
« Reply #5 on: July 25, 2016, 01:15:37 am »
Thanks to you too eXpl0it3r