Ошибка C++: точка не была объявлена ​​в этой области

Пожалуйста, помогите с этой ошибкой. Я использую компилятор 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 "В этой области была объявлена ​​точка". Может ли кто-нибудь помочь с этими ошибками, они действительно мешают моему прогрессу >.‹

Вот фактическая функция, которая находится внутри другого файла .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

Если вы можете помочь мне исправить это, это было бы потрясающе. Спасибо.


person Shasaur Aura    schedule 23.12.2012    source источник


Ответы (1)


У вас есть рекурсивное включение. прототипы.h включает в себя Dot.h, а Dot.h включает в себя прототипы.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
person Vaughn Cato    schedule 23.12.2012
comment
Решение Вона по прямому объявлению Dot намного лучше, чем включение файла .h. В больших системах время компиляции может зависеть от стоимости чтения файлов .h в каждую единицу компиляции. Выработав эту привычку, вы улучшите свою жизнь в будущем - person Nick; 24.12.2012