C++ 在';{';

C++ 在';{';,c++,sdl,C++,Sdl,我遇到了一个非常恼人的错误。我真的到处找它!我甚至回去改变了我所有的想法 if (case) // to-do 到 我不会问很多这样的问题,但我真的很沮丧,我几乎可以肯定这是一件我没有看到的简单的事情 以下是错误: entity.cpp: In member function ‘virtual void Entity::clean()’: entity.cpp:148: error: a function-definition is not allowed here before ‘{

我遇到了一个非常恼人的错误。我真的到处找它!我甚至回去改变了我所有的想法

if (case)
    // to-do

我不会问很多这样的问题,但我真的很沮丧,我几乎可以肯定这是一件我没有看到的简单的事情

以下是错误:

entity.cpp: In member function ‘virtual void Entity::clean()’:
entity.cpp:148: error: a function-definition is not allowed here before ‘{’ token
entity.cpp:394: error: expected ‘}’ at end of input
这是我的班级代码:

#include "./entity.hpp"

std::vector<Entity *> Entity::entity_list_;
std::vector<EntityCollision> EntityCollision::collision_list_;

EntityCollision::EntityCollision()
{
  a_ = NULL;
  b_ = NULL;
}

Entity::Entity()
{
  image_buffer_ = NULL;
  x_ = y_ = 0.0f;
  width_ = height_ = 0;
  animation_state_ = 0;
  move_left_ = false;
  move_right_ = false;
  type_ = ENTITY_TYPE_GENERIC;
  flags_ = ENTITY_FLAG_GRAVITY;
  dead_ = false;
  speed_x_ = 0;
  speed_y_ = 0;
  max_speed_x_ = 0;
  max_speed_y_ = 0;
  column_x_ = 0;
  column_y_ = 0;
  column_width_ = 0;
  column_height_ = 0;
}

Entity::~Entity()
{
}

bool Entity::init(const std::string &image_file, int width, int height, int max_frames)
{
  if ((image_buffer_ = Surface::mount_image(image_file)) == NULL)
    {
      return false;
    }

  animation_helper_.max_frames_ = max_frames;

  width_ = width;
  height_ = height;
  return Surface::set_color_key(255, 0, 255, image_buffer_);
}

void Entity::render(SDL_Surface *dest)
{
  if (image_buffer_ == NULL || dest == NULL)
    {
      return;
    }

  Surface::draw(x_ - CameraManager::camera_controller_.x(),
        y_ - CameraManager::camera_controller_.y(),
        current_frame_column_ * width_,
        (current_frame_row_ + animation_helper_.current_frame()) * height_,
        width_, height_,
        image_buffer_,
        dest);
}

void Entity::animate()
{
  if (move_left_)
    {
      current_frame_column_ = 0;
    }
  else if (move_right_)
    {
      current_frame_column_ = 1;
    }

  animation_helper_.animate();
}

void Entity::collision(Entity *entity)
{
}

void Entity::update()
{
  /* if the entity isn't moving left or right */
  if (move_left_ == false && move_right_ == false)
    {
      stop(); // stop movement
    }

  if (move_left_) // if it wants to move left
    {
      accel_x_ = -0.5; // move negatively down x axis
    }
  else if (move_right_)
    {
      accel_x_ = 0.5; // move positively up the x axis
    }

  /* if gravity is applied to the entity */
  if (flags_ & ENTITY_FLAG_GRAVITY)
    {
      accel_y_ = 0.75f; // it will fall
    }

  /* set the entity's speed */
  speed_x_ += accel_x_ * FPSManager::fps_controller_.speed_factor();
  speed_y_ += accel_y_ * FPSManager::fps_controller_.speed_factor();

  /* make sure the entity won't move too fast */
  if (speed_x_ > max_speed_x_)
    {
      speed_x_ = max_speed_x_;
    }

  if (speed_x_ < -max_speed_x_)
    {
      speed_x_ = -max_speed_x_;
    }

  if (speed_y_ > max_speed_y_)
    {
      speed_y_ = max_speed_y_;
    }

  if (speed_y_ < -max_speed_y_)
    {
      speed_y_ = -max_speed_y_;
    }

  animate(); // animate the entity
  move(speed_x_, speed_y_); // now move it
}

void Entity::clean()
{
  if (image_buffer_) // if the image buffer has an image on it
    {
      SDL_FreeSurface(image_buffer_); // get rid of it!
    }

  image_buffer_ = NULL; // just set it to null if it doesn't :\
}

void Entity::move(float x, float y)
{
  /* if thee entity  doesn't want to move, why try? */
  if (x == 0 && y == 0)
    {
      return;
    }

  double new_x = 0; // the x position increment
  double new_y = 0; // the y position increment

  /* give us the correct movement per second */
  x *= FPSManager::fps_controller_.speed_factor();
  y *= FPSManager::fps_controller_.speed_factor();

  if (y != 0) // if we should move up or down
    {
      if (y >= 0) // down in this case
      {
        new_y = FPSManager::fps_controller_.speed_factor();
      }
        else // up in this case
      {
        new_y = -FPSManager::fps_controller_.speed_factor();
      }
    }

  while (true)
    {
      if (flags_ & ENTITY_FLAG_GHOST) // is the entity a ghost?
      {
        /* notify the entity of other collisions */
        valid_pos((int) (x_ + new_x), (int) (y_ + new_y));
        /* move regardless of the results */
        x_ += new_x_;
        y_ += new_y_;
      }
      else
      {
        /* if the new y position is valid (empty) */
        if (valid_pos((int) (x_ + new_x), (int) y_))
          {
            x_ += new_x; // move the entity
          }
        else
          {
            speed_x_ = 0;
          }

      /* if the new y position is valid (empty) */
      if (valid_pos((int) x_, (int) (y_ + new_y)))
        {
          y_ += new_y; // move the entity
        }
      else
        {
          speed_y_ = 0;
        }
    }

      /* decrease x by new_x until it reaches 0 */
      x += -new_x;

      /* decrease y by new_y until it reaches 0 */
      y += -new_y;

      if (new_x > 0 && x <= 0)
      {
        new_x = 0;
      }

      if (new_x < 0 && x >= 0)
      {
        new_x = 0;
      }

      if (new_y > 0 && y <= 0)
      {
        new_y = 0;
      }

      if (new_y < 0 && y >= 0)
      {
        new_y = 0;
      }

      /* if the entity reached it's new position on the x axis */
      if (x == 0)
      {
        new_x = 0; // don't move left || right anymore
      }

      /* if the entity reached it's new position on the y axis */
      if (y == 0)
      {
        new_y = 0; // don't move up || down anymore
      }

      /* when we finally come to a stop */
      if (x == 0 && y == 0)
      {
        break; // break out of the loop
      }

      if (new_x == 0 && new_y == 0)
      {
        break;
      }
    }
}

void Entity::stop()
{
  if (speed_x_ > 0)
    {
      accel_x_ = -1;
    }

  if (speed_x_ < 0)
    {
      accel_x_ = 1;
    }

  if (speed_x_ < 2.0f && speed_x_ > -2.0f)
    {
      accel_x_ = 0;
      speed_x_ = 0;
    }
}

bool Entity::collides(int o_x, int o_y, int o_w, int o_h)
{
  int left1, left2;
  int right1, right2;
  int top1, int top2;
  int bottom1, int bottom2;

  int t_x = (int) x_ + column_x_;
  int t_y = (int) y_ + column_y_;

  left1 = t_x;
  left2 = o_x;

  right1 = left1 + width_ - 1 - column_width_;
  right2 = o_x + o_w - 1;

  top1 = t_y;
  top2 = o_y;

  bottom1 = top1 + height_ - 1 - column_height_;
  bottom2 = o_y + o_h - 1;

  if (bottom1 < top2)
    {
      return false;
    }

  if (top1 > bottom2)
    {
      return false;
    }

  if (right1 < left2)
    {
      return false;
    }

  if (left1 > right2)
    {
      return false;
    }

  return true;
}

bool valid_pos(int x, int y)
{
  bool _return = true;

  int start_x = (x + column_x_) / TILE_SIZE;
  int start_y = (y + column_y_) / TILE_SIZE;

  int end_x = ((x + column_x_) + width_ - column_width_ - 1) / TILE_SIZE;
  int end_y = ((x + column_y_) + height_ - column_height_ - 1) / TILE_SIZE;

  for (int i_y = start_y; i_y < end_y; i_y++)
    {
      for (int i_x = start_x; i_x < end_x; i_x++)
      {
        Tile *tile = MapManager::map_control_.get_tile(i_x * TILE_SIZE,
                             i_y * TILE_SIZE);
        if (valid_pos_tile(tile) == false)
          {
            _return = false;
          }
      }
    }

  if (flags_ & ENTITY_FLAG_MAPONLY)
    {
    }
  else
    {
      for (int i = 0; i < entity_list_.size(); i++)
      {
        if (!valid_pos_entity(entity_list_[i], x, y))
          {
            _return = false;
          }
      }
    }

  return _return;
}

bool valid_pos(Tile *t)
{
  if (t == NULL)
    {
      return true;
    }

  if (tile->type_id_ == TILE_TYPE_BLOCK)
    {
      return false;
    }

  return true;
}

bool valid_pos(Entity *e, int x, int y)
{
  if ((this != e) && (e != NULL) && (e->dead_ == false) &&
      (e->flags_ ^ ENTITY_FLAG_MAPONLY) &&
      (e->collides(x + column_x, y + column_y,
          width_ - column_width_ - 1,
           height_ - column_height_ - 1) == true))
    {
      EntityCollision ec;
      ec.a_ = this;
      ec.b_ = e;

      EntityCollision::collision_list_.push_back(ec);
      return false;
    }

  return true;
}
#包括“/entity.hpp”
std::vector Entity::Entity\u list\u;
std::vector EntityCollision::碰撞列表;
EntityCollision::EntityCollision()
{
a=空;
b_u=NULL;
}
实体::实体()
{
图像缓冲区=空;
x=y=0.0f;
宽度=高度=0;
动画状态=0;
向左移动=错误;
向右移动=错误;
类型=实体类型\u泛型;
标志=实体标志重力;
死亡=虚假;
速度x=0;
速度y=0;
最大速度x=0;
最大速度=0;
列x=0;
列_y_=0;
列宽=0;
列高度=0;
}
实体::~实体()
{
}
bool实体::init(常量std::字符串和图像文件,整数宽度,整数高度,整数最大帧)
{
if((图像\缓冲区\表面::装入\图像(图像\文件))==NULL)
{
返回false;
}
动画\辅助对象\最大\帧\最大\帧;
宽度=宽度;
高度=高度;
返回曲面::设置颜色键(255、0、255、图像缓冲区);
}
void实体::渲染(SDL_曲面*dest)
{
if(image_buffer_==NULL | | dest==NULL)
{
返回;
}
Surface::draw(x_u-CameraManager::camera_控制器_uu.x(),
y_u-CameraManager::camera_控制器u.y(),
当前帧列宽度,
(当前\帧\行\动画\辅助对象\当前\帧())*高度,
宽度、高度、,
图像缓冲区,
目的地);
}
void实体::动画()
{
如果(向左移动)
{
当前帧列=0;
}
else if(向右移动)
{
当前帧列=1;
}
动画u helper_u.animate();
}
void实体::冲突(实体*实体)
{
}
void实体::update()
{
/*如果实体没有向左或向右移动*/
if(向左移动=false和向右移动=false)
{
停止();//停止移动
}
if(move_left)//如果它想向左移动
{
加速度x=-0.5;//沿x轴负向移动
}
else if(向右移动)
{
加速度x=0.5;//沿x轴正向上移动
}
/*如果将重力应用于图元*/
if(标志\实体\标志\重力)
{
加速度=0.75f;//它会下降
}
/*设置实体的速度*/
速度x+=加速度x*fps管理器::fps u控制器u.速度系数();
速度=加速度*fps管理器::fps\控制器\速度系数();
/*确保实体不会移动得太快*/
如果(速度>最大速度)
{
速度=最大速度;
}
如果(速度<-最大速度)
{
速度x=-最大速度x;
}
如果(速度>最大速度)
{
速度=最大速度;
}
如果(速度<-最大速度<-
{
速度=最大速度;
}
animate();//设置实体的动画
移动(速度x,速度y);//现在移动它
}
void实体::clean()
{
if(image\u buffer)//如果图像缓冲区上有图像
{
SDL_FreeSurface(image_buffer);//摆脱它!
}
image\u buffer\uu=NULL;//如果没有,请将其设置为NULL:\
}
void实体::移动(浮动x,浮动y)
{
/*如果实体不想移动,为什么要尝试*/
如果(x==0&&y==0)
{
返回;
}
double new_x=0;//x位置增量
双新_y=0;//y位置增量
/*给我们每秒正确的移动速度*/
x*=fps管理器::fps_控制器_u.speed_factor();
y*=fps管理器::fps_控制器_u.speed_factor();
if(y!=0)//我们应该向上还是向下移动
{
如果(y>=0)//在这种情况下为向下
{
新建_y=fps管理器::fps_控制器_u.speed_factor();
}
在这种情况下,其他人都不同意
{
new_y=-FPSManager::fps_控制器_u.speed_factor();
}
}
while(true)
{
if(flags\u&ENTITY\u FLAG\u GHOST)//实体是重影吗?
{
/*通知实体其他冲突*/
有效位置((int)(x+新位置),(int)(y+新位置));
/*不管结果如何都要移动*/
x_u+=新的x_uu;
y_u+=新的y_u;
}
其他的
{
/*如果新y位置有效(空)*/
如果(有效位置((int)(x+new_x),(int)y_x))
{
x+=new\u x;//移动实体
}
其他的
{
速度x=0;
}
/*如果新y位置有效(空)*/
if(有效位置((int)x,(int)(y+new_y)))
{
y+=new\u y;//移动实体
}
其他的
{
速度y=0;
}
}
/*将x减少新的_x,直到它达到0*/
x+=-新的_x;
/*按新的y减少y,直到它达到0*/
y+=-新的y;
如果(新x>0&&x=0)
{
新的_x=0;
}
如果(新建y>0&&y=0)
{
新的y=0;
}
/*如果实体到达其在x轴上的新位置*/
如果(x==0)
{
new_x=0;//不再向左| |向右移动
}
/*如果实体到达y轴上的新位置*/
如果(y==0)
{
new_y=0;//不要再上移| |下移
}
/*当我们终于停下来的时候*/
如果(x==0&&y==0)
{
break;//中断循环
}
if(new_x==0&&new_y==0)
{
打破
}
}
}
void实体::stop()
{
如果(速度大于0)
{
加速度x=-1;
}
如果(速度x<0)
{
加速度x=1;
}
如果(速度<2.0f和速度>2.0f)
{
加速度x=0;
速度x=0;
}
}
布尔实体::碰撞(into_x,into_y,into_w,into_h)
{
int left1,left2;
int right1,right2;
int top1,int top2;
int-bottom1,int-bottom2;
int t_x=(int)x_ux+列_x;
int t_y=(int)y_+列_y;
left1=t_x;
left2=o_x;
对1=
#include "./entity.hpp"

std::vector<Entity *> Entity::entity_list_;
std::vector<EntityCollision> EntityCollision::collision_list_;

EntityCollision::EntityCollision()
{
  a_ = NULL;
  b_ = NULL;
}

Entity::Entity()
{
  image_buffer_ = NULL;
  x_ = y_ = 0.0f;
  width_ = height_ = 0;
  animation_state_ = 0;
  move_left_ = false;
  move_right_ = false;
  type_ = ENTITY_TYPE_GENERIC;
  flags_ = ENTITY_FLAG_GRAVITY;
  dead_ = false;
  speed_x_ = 0;
  speed_y_ = 0;
  max_speed_x_ = 0;
  max_speed_y_ = 0;
  column_x_ = 0;
  column_y_ = 0;
  column_width_ = 0;
  column_height_ = 0;
}

Entity::~Entity()
{
}

bool Entity::init(const std::string &image_file, int width, int height, int max_frames)
{
  if ((image_buffer_ = Surface::mount_image(image_file)) == NULL)
    {
      return false;
    }

  animation_helper_.max_frames_ = max_frames;

  width_ = width;
  height_ = height;
  return Surface::set_color_key(255, 0, 255, image_buffer_);
}

void Entity::render(SDL_Surface *dest)
{
  if (image_buffer_ == NULL || dest == NULL)
    {
      return;
    }

  Surface::draw(x_ - CameraManager::camera_controller_.x(),
        y_ - CameraManager::camera_controller_.y(),
        current_frame_column_ * width_,
        (current_frame_row_ + animation_helper_.current_frame()) * height_,
        width_, height_,
        image_buffer_,
        dest);
}

void Entity::animate()
{
  if (move_left_)
    {
      current_frame_column_ = 0;
    }
  else if (move_right_)
    {
      current_frame_column_ = 1;
    }

  animation_helper_.animate();
}

void Entity::collision(Entity *entity)
{
}

void Entity::update()
{
  /* if the entity isn't moving left or right */
  if (move_left_ == false && move_right_ == false)
    {
      stop(); // stop movement
    }

  if (move_left_) // if it wants to move left
    {
      accel_x_ = -0.5; // move negatively down x axis
    }
  else if (move_right_)
    {
      accel_x_ = 0.5; // move positively up the x axis
    }

  /* if gravity is applied to the entity */
  if (flags_ & ENTITY_FLAG_GRAVITY)
    {
      accel_y_ = 0.75f; // it will fall
    }

  /* set the entity's speed */
  speed_x_ += accel_x_ * FPSManager::fps_controller_.speed_factor();
  speed_y_ += accel_y_ * FPSManager::fps_controller_.speed_factor();

  /* make sure the entity won't move too fast */
  if (speed_x_ > max_speed_x_)
    {
      speed_x_ = max_speed_x_;
    }

  if (speed_x_ < -max_speed_x_)
    {
      speed_x_ = -max_speed_x_;
    }

  if (speed_y_ > max_speed_y_)
    {
      speed_y_ = max_speed_y_;
    }

  if (speed_y_ < -max_speed_y_)
    {
      speed_y_ = -max_speed_y_;
    }

  animate(); // animate the entity
  move(speed_x_, speed_y_); // now move it
}

void Entity::clean()
{
  if (image_buffer_) // if the image buffer has an image on it
    {
      SDL_FreeSurface(image_buffer_); // get rid of it!
    }

  image_buffer_ = NULL; // just set it to null if it doesn't :\
}

void Entity::move(float x, float y)
{
  /* if thee entity  doesn't want to move, why try? */
  if (x == 0 && y == 0)
    {
      return;
    }

  double new_x = 0; // the x position increment
  double new_y = 0; // the y position increment

  /* give us the correct movement per second */
  x *= FPSManager::fps_controller_.speed_factor();
  y *= FPSManager::fps_controller_.speed_factor();

  if (y != 0) // if we should move up or down
    {
      if (y >= 0) // down in this case
      {
        new_y = FPSManager::fps_controller_.speed_factor();
      }
        else // up in this case
      {
        new_y = -FPSManager::fps_controller_.speed_factor();
      }
    }

  while (true)
    {
      if (flags_ & ENTITY_FLAG_GHOST) // is the entity a ghost?
      {
        /* notify the entity of other collisions */
        valid_pos((int) (x_ + new_x), (int) (y_ + new_y));
        /* move regardless of the results */
        x_ += new_x_;
        y_ += new_y_;
      }
      else
      {
        /* if the new y position is valid (empty) */
        if (valid_pos((int) (x_ + new_x), (int) y_))
          {
            x_ += new_x; // move the entity
          }
        else
          {
            speed_x_ = 0;
          }

      /* if the new y position is valid (empty) */
      if (valid_pos((int) x_, (int) (y_ + new_y)))
        {
          y_ += new_y; // move the entity
        }
      else
        {
          speed_y_ = 0;
        }
    }

      /* decrease x by new_x until it reaches 0 */
      x += -new_x;

      /* decrease y by new_y until it reaches 0 */
      y += -new_y;

      if (new_x > 0 && x <= 0)
      {
        new_x = 0;
      }

      if (new_x < 0 && x >= 0)
      {
        new_x = 0;
      }

      if (new_y > 0 && y <= 0)
      {
        new_y = 0;
      }

      if (new_y < 0 && y >= 0)
      {
        new_y = 0;
      }

      /* if the entity reached it's new position on the x axis */
      if (x == 0)
      {
        new_x = 0; // don't move left || right anymore
      }

      /* if the entity reached it's new position on the y axis */
      if (y == 0)
      {
        new_y = 0; // don't move up || down anymore
      }

      /* when we finally come to a stop */
      if (x == 0 && y == 0)
      {
        break; // break out of the loop
      }

      if (new_x == 0 && new_y == 0)
      {
        break;
      }
    }
}

void Entity::stop()
{
  if (speed_x_ > 0)
    {
      accel_x_ = -1;
    }

  if (speed_x_ < 0)
    {
      accel_x_ = 1;
    }

  if (speed_x_ < 2.0f && speed_x_ > -2.0f)
    {
      accel_x_ = 0;
      speed_x_ = 0;
    }
}

bool Entity::collides(int o_x, int o_y, int o_w, int o_h)
{
  int left1, left2;
  int right1, right2;
  int top1, int top2;
  int bottom1, int bottom2;

  int t_x = (int) x_ + column_x_;
  int t_y = (int) y_ + column_y_;

  left1 = t_x;
  left2 = o_x;

  right1 = left1 + width_ - 1 - column_width_;
  right2 = o_x + o_w - 1;

  top1 = t_y;
  top2 = o_y;

  bottom1 = top1 + height_ - 1 - column_height_;
  bottom2 = o_y + o_h - 1;

  if (bottom1 < top2)
    {
      return false;
    }

  if (top1 > bottom2)
    {
      return false;
    }

  if (right1 < left2)
    {
      return false;
    }

  if (left1 > right2)
    {
      return false;
    }

  return true;
}

bool valid_pos(int x, int y)
{
  bool _return = true;

  int start_x = (x + column_x_) / TILE_SIZE;
  int start_y = (y + column_y_) / TILE_SIZE;

  int end_x = ((x + column_x_) + width_ - column_width_ - 1) / TILE_SIZE;
  int end_y = ((x + column_y_) + height_ - column_height_ - 1) / TILE_SIZE;

  for (int i_y = start_y; i_y < end_y; i_y++)
    {
      for (int i_x = start_x; i_x < end_x; i_x++)
      {
        Tile *tile = MapManager::map_control_.get_tile(i_x * TILE_SIZE,
                             i_y * TILE_SIZE);
        if (valid_pos_tile(tile) == false)
          {
            _return = false;
          }
      }
    }

  if (flags_ & ENTITY_FLAG_MAPONLY)
    {
    }
  else
    {
      for (int i = 0; i < entity_list_.size(); i++)
      {
        if (!valid_pos_entity(entity_list_[i], x, y))
          {
            _return = false;
          }
      }
    }

  return _return;
}

bool valid_pos(Tile *t)
{
  if (t == NULL)
    {
      return true;
    }

  if (tile->type_id_ == TILE_TYPE_BLOCK)
    {
      return false;
    }

  return true;
}

bool valid_pos(Entity *e, int x, int y)
{
  if ((this != e) && (e != NULL) && (e->dead_ == false) &&
      (e->flags_ ^ ENTITY_FLAG_MAPONLY) &&
      (e->collides(x + column_x, y + column_y,
          width_ - column_width_ - 1,
           height_ - column_height_ - 1) == true))
    {
      EntityCollision ec;
      ec.a_ = this;
      ec.b_ = e;

      EntityCollision::collision_list_.push_back(ec);
      return false;
    }

  return true;
}
  image_buffer_ = NULL; // just set it to null if it doesn't :\
}
  image_buffer_ = NULL; // just set it to null if it doesn't :\
}
    image_buffer_ = NULL; // just set it to null if it doesn't :\
}