gpt4 book ai didi

C++ 错误 : 'Dot' was not declared in this scope

转载 作者:搜寻专家 更新时间:2023-10-31 01:51:16 25 4
gpt4 key购买 nike

请帮忙解决这个错误。我正在使用一个名为 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

当我尝试在 header 中声明带有 Dot 参数的原型(prototype)时,我遇到了这个烦人的错误。 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

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

最佳答案

你有一个递归包含。 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

关于C++ 错误 : 'Dot' was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14014603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com