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

Author Topic: using namespace sf; error  (Read 6068 times)

0 Members and 1 Guest are viewing this topic.

nopilpl

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
using namespace sf; error
« on: January 30, 2018, 03:50:13 pm »
Hey
When i want build my code compiler shows the error:
Severity   Code   Description   Project   File   Line   Suppression State
Error   C2871   'sf': a namespace with this name does not exist   ConsoleApplication11   c:\users\kamil\documents\visual studio 2015\projects\consoleapplication11\consoleapplication11\game.cpp
Severity   Code   Description   Project   File   Line   Suppression State 9

ConsoleApplication11.cpp
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\System.hpp>
#include <Windows.h>
#include <string>
#include "stdafx.h"
#include "Game.h"


int main()
{
        Game game;
        game.runGame();

        return EXIT_SUCCESS;
}

Game.cpp
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\System.hpp>
#include <Windows.h>
#include <string>
#include "Game.h"
#include "stdafx.h"

using namespace sf;

RenderWindow window(VideoMode(1280, 720), "Morbid Squirrel Wars");

Game::Game(void)
{
        state = END;

        if (!font.loadFromFile("Fonts/arial.ttf"))
        {
                //Error
        }

        state = MENU;
}


void Game::move()
{
        if (state == GAME)
        {
                Texture tPlayer;
                tPlayer.loadFromFile("Sprites/Player");
                Sprite sPlayer;
                sPlayer.setTexture(tPlayer);




        }





}

void Game::runGame()
{
        while (state != END)
        {
                switch (state)
                {
                case GameState::MENU:
                        menu();
                        break;
                case GameState::GAME:
                        //
                        break;
                }
        }
}


void Game::menu()
{
        Text Title;
        Title.setStyle(Text::Bold);
        Title.setFont(font);
        Title.setCharacterSize(80);
        Title.setString("Morbid Squirrel Wars");
        Title.setPosition(1280 / 2 - Title.getGlobalBounds().width / 2, 20);


        Text Play;
        Play.setFont(font);
        Play.setCharacterSize(65);
        Play.setString("Play");
        Play.setPosition(1280 / 2 - Play.getGlobalBounds().width / 2, 250);

        Text Options;
        Options.setFont(font);
        Options.setCharacterSize(65);
        Options.setString("Options");
        Options.setPosition(1280 / 2 - Options.getGlobalBounds().width / 2, 370);

        Text Exit;
        Exit.setFont(font);
        Exit.setCharacterSize(65);
        Exit.setString("Exit");
        Exit.setPosition(1280 / 2 - Exit.getGlobalBounds().width / 2, 490);

        while (state == MENU)
        {
                Vector2f mouse(Mouse::getPosition(window));
                Event event;

                if (state == END)
                {
                        window.close();
                }
                while (window.pollEvent(event))
                {
                        //klikniêcie EXIT
                        if (Exit.getGlobalBounds().contains(mouse) &&
                                event.type == Event::MouseButtonReleased && event.key.code == Mouse::Left)
                        {
                                state = END;
                        }

                        if (Play.getGlobalBounds().contains(mouse))
                                Play.setColor(Color::Cyan);
                        else Play.setColor(Color::White);

                        if (Options.getGlobalBounds().contains(mouse))
                                Options.setColor(Color::Cyan);
                        else Options.setColor(Color::White);

                        if (Exit.getGlobalBounds().contains(mouse))
                                Exit.setColor(Color::Cyan);
                        else Exit.setColor(Color::White);
                }

                if (Play.getGlobalBounds().contains(mouse) && event.type == Event::MouseButtonReleased && event.key.code == Mouse::Left)
                {
                        state = GAME;
                }

                window.clear();

                window.draw(Title);
                window.draw(Play);
                window.draw(Options);
                window.draw(Exit);

                window.display();
        }
}


Game::~Game()
{
}

Game.h
#pragma once
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\System.hpp>
#include <Windows.h>
#include <string>
#include "stdafx.h"

using namespace std;
using namespace sf;

class Game
{
public:
        Game(void);
        ~Game(void);

        void move();
        void runGame();
        enum GameState { MENU, GAME, GAME_OVER, END };
        GameState state;

        Font font;
        void menu();
};

 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: using namespace sf; error
« Reply #1 on: January 30, 2018, 04:10:16 pm »
Simple solution: Don't use using namespace sf; or using namespace std; for that matter. It will make your code more readable and you won't run into annoying conflicts further down the line.

If you don't want to follow that advice, then at least don't use it in headers.

And for your problem, don't use it twice (header and source file).

But really, don't use it at all. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

nopilpl

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: using namespace sf; error
« Reply #2 on: January 30, 2018, 04:24:31 pm »
When I delete using namespace sf; and I use sf:: it shows me this error
Severity   Code   Description   Project   File   Line   Suppression State
Error   C2653   'sf': is not a class or namespace name   ConsoleApplication11   c:\users\kamil\documents\visual studio 2015\projects\consoleapplication11\consoleapplication11\game.cpp   10   

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: using namespace sf; error
« Reply #3 on: January 30, 2018, 05:08:22 pm »
My next guess would be that #include "stdafx.h" is messing things up.
Personally, I tend to avoid using pre-compiled headers (unless I have a huge project).

Additionally, you can't have a global render window. SFML resource must not be globally initialized.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

nopilpl

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: using namespace sf; error
« Reply #4 on: January 30, 2018, 05:24:21 pm »
OK, it work thanks eXpl0it3r
« Last Edit: January 30, 2018, 05:50:48 pm by nopilpl »

 

anything