命名空间'sf'中的X未命名类型

时间:2012-07-25 10:48:21

标签: c++ sfml

我正在使用SFML1.6来制作图形应用程序(游戏)。当我只有一个文件超出主文件时,它工作正常。现在我添加了一个新文件Level,我收到了这些错误:

C:\Users\...\Window.hpp|6|error: 'RenderWindow' in namespace 'sf' does not name a type|
C:\Users\...\Window.hpp|7|error: 'Image' in namespace 'sf' does not name a type|
C:\Users\...\Window.hpp|8|error: 'Sprite' in namespace 'sf' does not name a type|
C:\Users\...\Window.hpp|9|error: 'Color' in namespace 'sf' does not name a type|

由于它工作正常,我认为这是由于新文件:

Level.cpp

#include <SFML/Window.hpp>
#include <iostream>

#include "Window.hpp"

class Level
{
     sf::Window* pResultScreen;
     public:
     Level( sf::Window& );
}; 

Level::Level( sf::Window &app)
{
     pResultScreen = &app;
};

Level.hpp

#ifndef Level_hpp
#define Level_hpp

class Level
{
     sf::Window* pResultScreen;
     public:
     Level( sf::Window& );
};

#endif

有没有人发现上述代码存在问题?

编辑: Window.cpp

class Window
{
sf::RenderWindow appWindow;
sf::Image charImage;
sf::Sprite charSpriteSheet;
sf::Color deleteColor;
bool keyPressed;
int curFrame;
int stateChar;
int timerFrame;
int flipChar;

public:
Window();
int Loop();
void SheetCutter(int); // Prepares tiles
void Animate(int); // Loop through animations
void ColourCleaner(); // Remove the sprite's color.
};

Window::Window()
{
stateChar = 0;
curFrame = 0;
flipChar = 1;

deleteColor.a = 255;
deleteColor.r = 255;
deleteColor.g = 47;
deleteColor.b = 198;

appWindow.Create(sf::VideoMode( 800, 600, 32), "AI_Fighter");
appWindow.SetFramerateLimit(30);

if( !charImage.LoadFromFile("Bass.png") )
{
    std::cout << "Problem opening file 'Bass.png'";
}

charImage.SetSmooth(false);

ColourCleaner();

charSpriteSheet.SetImage(charImage);

SheetCutter(curFrame); // This allows it to not show the entire tile set at the    beginning.
};

int Window::Loop()
{
// Start game loop
while (appWindow.IsOpened())
{
    sf::Event evt;
    while (appWindow.GetEvent(evt))
    {
        // Window closed
        if (evt.Type == sf::Event::Closed)
            appWindow.Close();

        // Escape key pressed
        if ((evt.Type == sf::Event::KeyPressed) && (evt.Key.Code == sf::Key::Escape))
            appWindow.Close();

        if (evt.Type == sf::Event::KeyPressed)
            keyPressed = true;

        else
            keyPressed = false;
    }

    // Get elapsed time
    float ElapsedTime = appWindow.GetFrameTime();

    // Move the sprite
    if ( appWindow.GetInput().IsKeyDown(sf::Key::Left) || appWindow.GetInput().IsKeyDown(sf::Key::Right) || appWindow.GetInput().IsKeyDown(sf::Key::Up) || appWindow.GetInput().IsKeyDown(sf::Key::Down) )
    {
        if (appWindow.GetInput().IsKeyDown(sf::Key::Left))
        {
            charSpriteSheet.Move(-100 * ElapsedTime, 0);
            stateChar = 1;
            flipChar = 0;

            Animate(6);
        }

        if (appWindow.GetInput().IsKeyDown(sf::Key::Right))
        {
            charSpriteSheet.Move( 100 * ElapsedTime, 0);
            stateChar = 1;
            flipChar = 1;

            Animate(6);
        }

        if (appWindow.GetInput().IsKeyDown(sf::Key::Up))
        {
            charSpriteSheet.Move(0, -100 * ElapsedTime);
            stateChar = 2;

            Animate(6);
        }

        if (appWindow.GetInput().IsKeyDown(sf::Key::Down))  charSpriteSheet.Move(0,  100 * ElapsedTime);
    }

    else
    {
        stateChar = 0;

        Animate(12);
    }

    // Clear the screen (fill it with black color)
    appWindow.Clear(sf::Color( 0, 0, 0));

    appWindow.Draw(charSpriteSheet);

    // Display window contents on screen
    appWindow.Display();
}

return 0;
};

void Window::SheetCutter(int offset)
{
charSpriteSheet.SetSubRect(sf::IntRect((offset * 26), 0, ((offset * 26) + 26), 35));
};

// This changes the curFrame, so that animations loop and stuff. Pretty easy, although    unelegant.
void Window::Animate(int rate)
{
charSpriteSheet.FlipX(flipChar);

if((timerFrame%rate) == 0) // This is, rather obviously, a variable designed to allow to change the frames at the right rate.
{
    if(stateChar == 0) // ? Not getting activated. Not getting called...
    {
        if(curFrame >= 2)
        {
            curFrame = 0;
        }

        else
        {
            curFrame++;
        }
    }

    else if(stateChar == 1) // Walking.
    {
        if(curFrame >= 5 || curFrame <= 2 )
        {
            curFrame = 3;
        }

        else
        {
            curFrame++;
        }
    }

    else curFrame = 6; // Jump.

    SheetCutter(curFrame);
}

std::cout << stateChar;
timerFrame++;
};

// This removes the magenta background, changing the colour to tempClr. Color doesn't        really matter as long as the fourth digit is set
// to 0, since it's the alpha value.
void Window::ColourCleaner()
{
sf::Color tempClr(0, 0, 0, 0);

for(int i = 0; i < 182; i++ )
{
    for(int j = 0; j < 35; j++)
    {
        sf::Color CLR = charImage.GetPixel(i, j);
        if(deleteColor == CLR)
        {
            charImage.SetPixel(i, j, tempClr);
        }
    }
}
};

Window.hpp

#ifndef Window_hpp
#define Window_hpp

class Window
{
sf::RenderWindow appWindow;
sf::Image charImage;
sf::Sprite charSpriteSheet;
sf::Color deleteColor;
int curFrame;
int stateChar;
int timerFrame;

public:
Window();
int Loop();
void SheetCutter();
void Animate(int rate);
void ColourCleaner();
};

#endif

1 个答案:

答案 0 :(得分:1)

您应该将#include <SFML/Window.hpp>放入头文件中,因为您需要在其中定义。 (然后源文件应该包含标题而不是复制其内容