【问题标题】:c Allegro ISO error Using & and integers yet not workingc Allegro ISO 错误使用 & 和整数但不工作
【发布时间】:2012-10-25 21:53:33
【问题描述】:

我正在使用自己的逻辑和原始引擎制作快板游戏。我试图使重力和接触阵列中的瓷砖。为了能够从另一个类更改一个类的值,我必须使用引用符号“&”。编译后我收到此错误 在gravity.cpp的第29行,ISO C++禁止指针和整数的比较 这是重力类和gravity.cpp以及player.h和player.cpp的头文件。

重力.h

#ifndef GRAVITY_H
#define GRAVITY_H
#include<allegro.h>
#include<fstream>
#include "Global.h"

class Gravity
{
public:
    Gravity();
    ~Gravity();
    bool canJump,JumpAgain,OnSolidTile,Collision;
    int GravSpeed;
    void LoadMap(const char*filename);
    void Init();
    void Grav_Collision(int &x,int &y,int &x2,int &y2,int Gravspeed,bool canJump,bool JumpAgain,bool OnSolidTile,bool Collision);
    private:
    int loadCounterX,loadCounterY;
    int ColMap[100][100];
    int MapSizeX,MapSizeY;
    };

    #endif // GRAVITY_H

重力.cpp 此文件出错 #include "gravity.h" // 类的头文件

Gravity::Gravity()
{

}


Gravity::~Gravity()
{

}
void Gravity::Init()
{
 loadCounterX = loadCounterY = 0;
};
void Gravity::Grav_Collision(int &x,int &y,int &x2,int &y2,int Gravspeed,bool canJump,bool JumpAgain,bool OnSolidTile,bool Collision)
{
 for(int i = 0;i < MapSizeX;i++)
 {
     for(int j = 0;j < MapSizeY;j++)
     {
      if(ColMap[i][j] = 1)
      {
          //Collision
          if(&y2 < j*BlockSize)--**This IS where the error is found **--
          {
            Collision = true;       
          }
          else
          {
           Collision = false;    
          }      
      }        
     }        
 }
};
 void Gravity::LoadMap(const char*filename)
 {
 std::ifstream openfile (filename);
 if (openfile.is_open())
 {
 openfile >> MapSizeX >> MapSizeY;
 while (!openfile.eof())
 {
     openfile >> ColMap[loadCounterX][loadCounterY];   
     loadCounterX ++;
     if (loadCounterX >= MapSizeX)
     {
        loadCounterX = 0;
        loadCounterY ++;
     }
 }                       
   loadCounterX = loadCounterY = 0;
 } 
 else
 {
 allegro_message ("NO Collision FILE");    
 } 
 };

播放器.h

#ifndef PLAYER_H
#define PLAYER_H
#include <allegro.h>
#include "Global.h"

class player
{
public:
    player();
    ~player();
    void Init();
    void Update();
    void Draw(BITMAP*buffer,int c1,int c2,int c3);
    void Input();
    //Variable
    int x;
    int x2;
    int y;
    int y2;
    int speed;
    char dir;
    BITMAP*buffer;
    private:
    int c1;
    int c2;
    int c3;
    };

   #endif // PLAYER_H

播放器.cpp

#include "player.h" // class's header file

// class constructor
player::player()
{
// insert your code here
}

// class destructor
player::~player()
{
// insert your code here
}
void player::Init()
{
 x = 9;
 y = 9;
 speed = 1;
 dir = 0;
};
void player::Draw(BITMAP*buffer,int c1,int c2,int c3)
{
 textprintf(buffer, font, x-30, y-10, makecol(255,0,0), "x: %03i", x);
 textprintf(buffer, font, x-30, y-20, makecol(255,0,0), "y: %03i", y);
 rectfill(buffer,x,y,x + 5,y + 5,makecol(c1,c2,c3));
};
void player::Input()
{
 //Input
 if(KLeft && x >= 0)
 {
         dir = 'l'; 
 }
 else if(KRIGHT && x <= SW)
 {
       dir = 'r';      
 }
 else if(KUP && y >= 0)
 {
       dir = 'u';      
 }
 else if(KDOWN && y <= SH)
 {
       dir = 'd';      
 }
 else
 {
       dir = 'e';
 }
 //Output

 if(dir == 'l')
 {
     x += -speed;       
 }
 if(dir == 'r')
 {
   x  += speed;   
 }
 if(dir == 'u')
 {
     y += -speed;       
 }
 if(dir == 'd')
 {
   y  += speed;   
 }

};
void player::Update()
{
 player::Input();

};

【问题讨论】:

    标签: c++ allegro


    【解决方案1】:

    在错误行中,您可能指的是y2 &lt; j*BlockSize 而不是&amp;y2 &lt; j*BlockSize。您正在将整数与变量 y 的地址(即指针)进行比较。这正是编译器所抱怨的。

    【讨论】:

    • 是的,它有效。现在我必须修复我的重力引擎 :( 谢谢你的帮助。你们所有人
    • @WilliamChantrillPaul 不客气。欢迎来到stackoverflow。请接受对您的问题最有用的答案,以便其他人可以从中受益。还可以考虑对有用/无用的答案投赞成票/反对票。
    【解决方案2】:

    错误信息清楚地告诉你出了什么问题。

    在这一行:

    if(&y2 < j*BlockSize)
    

    因为y2 是一个整数&amp;y2 产生一个指针,而j*BlockSize 是一个整数。因此,编译器告诉您比较整数和指针是没有意义的。只需删除&amp;

    if(y2 < j*BlockSize)
    

    【讨论】:

      【解决方案3】:

      编译器告诉你所有你需要知道的事情,在那一行&amp;y2得到y2的地址,你正在将它与j*BlockSize进行比较,这是一个整体,编译器会告诉你为什么要比较地址有数字吗?

      老实说,我没有阅读您的代码,也不知道您在那里做什么,所以如果您想比较 y2j*BlockSize 的值,只需按 y2 &lt; j*BlockSize 进行,在 C++ 中引用不需要访问器,您可以将它们的值作为普通值读取/写入

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-16
        • 1970-01-01
        • 1970-01-01
        • 2014-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多