C ++错误:在此范围内未声明'Dot'

时间:2012-12-23 20:49:00

标签: c++ class sdl

请帮助解决此错误。我正在使用名为Code :: Blocks的编译器。 这是我的代码。

#ifndef PROTOTYPES_H_INCLUDED
#define PROTOTYPES_H_INCLUDED

//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <fstream>
#include <sstream>

#include "Dot.h"
#include "vars.h"

using namespace std;

bool load_files( Dot &thisDot, Uint32 &bg );

#endif // PROTOTYPES_H_INCLUDED

当我尝试在标题中声明一个带有Dot参数的原型时,我得到了这个恼人的错误。 s“在这个范围内声明了Dot”。任何人都可以帮助解决这些错误,他们确实阻碍了我的进展&gt;。&lt;

这是实际的函数,它位于同一目录中不同的.cpp文件中(不用担心链接,由编译器负责):

bool load_files( Dot &thisDot, Uint32 &bg ) {
    //Load the dot image
    dot = load_image( "spritesheet.png" );

    //Open font
    font = TTF_OpenFont( "lazy.ttf", 28 );

    //If there was a problem in loading the dot
    if( dot == NULL )
    {
        return false;
    }

    if( font == NULL ) {
        return false;
    }

    //Open a file for reading
    std::ifstream load( "game_save" );

    //If the file loaded
    if( load != NULL )
    {
        //The offset
        int offset;

        //The level name
        std::string level;

        //Set the x offset
        load >> offset;
        thisDot.set_x( offset );

        //Set the y offset
        load >> offset;
        thisDot.set_y( offset );

        //If the x offset is invalid
        if( ( thisDot.get_x() < 0 ) || ( thisDot.get_x() > SCREEN_WIDTH - DOT_WIDTH ) )
        {
            return false;
        }

        //If the y offset is invalid
        if( ( thisDot.get_y() < 0 ) || ( thisDot.get_y() > SCREEN_HEIGHT - DOT_HEIGHT ) )
        {
            return false;
        }

        //Skip past the end of the line
        load.ignore();

        //Get the next line
        getline( load, level );
        load >> DOT_DIRECTION;

        //If an error occurred while trying to read the data
        if( load.fail() == true )
        {
            return false;
        }

        //If the level was white
        if( level == "White Level" )
        {
            //Set the background color
            bg = SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF );
        }
        //If the level was red
        else if( level == "Red Level" )
        {
            //Set the background color
            bg = SDL_MapRGB( screen->format, 0xFF, 0x00, 0x00 );
        }
        //If the level was green
        else if( level == "Green Level" )
        {
            //Set the background color
            bg = SDL_MapRGB( screen->format, 0x00, 0xFF, 0x00 );
        }
        //If the level was blue
        else if( level == "Blue Level" )
        {
            //Set the background color
            bg = SDL_MapRGB( screen->format, 0x00, 0x00, 0xFF );
        }

        //Close the file
        load.close();
    }

    //If everything loaded fine
    return true;
}

这是'Dot.h',它与所有其他文件位于同一目录中:

#ifndef DOT_H_INCLUDED
#define DOT_H_INCLUDED

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <fstream>
#include <sstream>

#include "vars.h"
#include "prototypes.h"

//The dot
class Dot{
    private:
    //The X and Y offsets of the dot
    int x, y;

    //The velocity of the dot
    int xVel, yVel;

    //Game variables
    int chakra, maxChakra;

    public:
    //Initializes the variables
    Dot();

    //Takes key presses and adjusts the dot's velocity
    void handle_input();

    //Moves the dot
    void move();

    //Shows the dot on the screen
    void show();

    //Set the dot's x/y offsets
    void set_x( int X );
    void set_y( int Y );

    //Get the dot's x/y offsets
    int get_x();
    int get_y();

    //Chakra
    int getChakra();
    int getMaxChakra();
    void setChakra(int amount);
    void useChakra(int amount);
    void gainChakra(int amount);
};

Dot::Dot()
{
    //Initialize the offsets
    x = 0;
    y = 0;

    //Initialize the velocity
    xVel = 0;
    yVel = 0;

    //Initialize chakra
    maxChakra = 300;
    chakra = maxChakra;
}

void Dot::set_x( int X )
{
    x = X;
}

void Dot::set_y( int Y )
{
    y = Y;
}

int Dot::get_x()
{
    return x;
}

int Dot::get_y()
{
    return y;
}

int Dot::getChakra(){
    return chakra;
}

int Dot::getMaxChakra(){
    return maxChakra;
}

void Dot::setChakra(int amount){
    chakra = amount;
    //if (chakra<0) bg = SDL_MapRGB( screen->format, 0xFF, 0x00, 0x00 );
}

void Dot::useChakra(int amount){
    chakra -= amount;
    //if (chakra<0) bg = SDL_MapRGB( screen->format, 0xFF, 0x00, 0x00 );
}

void Dot::gainChakra(int amount){
    chakra += amount;
    if (chakra>maxChakra) chakra = maxChakra;
}

void Dot::handle_input()
{
    //If a key was pressed
    if( event.type == SDL_KEYDOWN)
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_UP:
            yVel -= DOT_HEIGHT / 2;
            if(!jutsuActive) DOT_DIRECTION = 0;
            break;

            case SDLK_DOWN:
            yVel += DOT_HEIGHT / 2;
            if(!jutsuActive) DOT_DIRECTION = 2;
            break;

            case SDLK_LEFT:
            xVel -= DOT_WIDTH / 2;
            if(!jutsuActive) DOT_DIRECTION = 3;
            break;

            case SDLK_RIGHT:
            xVel += DOT_WIDTH / 2;
            if(!jutsuActive) DOT_DIRECTION = 1;
            break;
            default: ;
        }
    }
    //If a key was released
    else if( event.type == SDL_KEYUP )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel += DOT_HEIGHT / 2; break;
            case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;
            case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;
            case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;
            default: ;
        }
    }
}

void Dot::move()
{
    //Move the dot left or right
    x += xVel;

    //If the dot went too far to the left or right
    if( ( x < 0 ) || ( x + DOT_WIDTH > SCREEN_WIDTH ) || jutsuActive )
    {
        //Move back
        x -= xVel;
    }

    //Move the dot up or down
    y += yVel;

    //If the dot went too far up or down
    if( ( y < 0 ) || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) || jutsuActive )
    {
        //Move back
        y -= yVel;
    }
}

void Dot::show()
{
    //Show the dot
    //apply_surface( x, y, dot, screen, &clipsChar[DOT_DIRECTION] );
}

#endif // DOT_H_INCLUDED

如果你能帮助我解决这个问题,那就太棒了。感谢。

1 个答案:

答案 0 :(得分:3)

你有一个递归的包含。 prototypes.h包括Dot.h,Dot.h包括prototypes.h。

因为警卫阻止了真正的递归,所以你只会收到意外的包含订单。

因为prototypes.h实际上并不需要Dot类的定义,所以你可以转发声明Dot:

#ifndef PROTOTYPES_H_INCLUDED
#define PROTOTYPES_H_INCLUDED

//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <fstream>
#include <sstream>

#include "vars.h"

using namespace std;
class Dot;

bool load_files( Dot &thisDot, Uint32 &bg );

#endif // PROTOTYPES_H_INCLUDED