我在编译代码时遇到以下错误:
1>Mapa.obj : error LNK2019: unresolved external symbol "public: void __thiscall Mapa::Collision(float,float,float,float,int,int,int,int)" (?Collision@Mapa@@QAEXMMMMHHHH@Z) referenced in function "public: void __thiscall Mapa::mapa1(float,float,int,struct ALLEGRO_BITMAP *,struct ALLEGRO_BITMAP *,int)" (?mapa1@Mapa@@QAEXMMHPAUALLEGRO_BITMAP@@0H@Z)
Heder File
class Mapa
{
public:
Mapa(void);
~Mapa(void);
void Mapa::mapa1(float xp, float yp,int dir,ALLEGRO_BITMAP *party, ALLEGRO_BITMAP *mapa1,int moveSpeed);
void Mapa::Collision(float xp,float yp,float ex,float ey,int width,int height,int dir,int moveSpeed);
//const float moveSpeed;
};
这是功能
Mapa::Mapa(void)
{
}
Mapa::~Mapa(void)
{
}
enum Direction { DOWN, LEFT, RIGHT, UP };
int dir = DOWN;
void Collision(float xp,float yp,float ex,float ey,int width,int height,int dir,int moveSpeed)
{
if( xp + width < ex || xp> ex+ width || yp+ height <ey || yp > ey +height)
{
}
else
{
{
if(dir==0)
yp-=moveSpeed;
else if(dir==1)
xp+=moveSpeed;
else if(dir==2)
xp-=moveSpeed;
else if(dir==3)
yp+=moveSpeed;
}
}
}
void Mapa::mapa1(float xp, float yp,int dir,ALLEGRO_BITMAP *party,ALLEGRO_BITMAP *mapa1,int moveSpeed)
{
bool draw=true;
bool done=false;
bool active=false;
int sourceX,sourceY;
const float FPS = 60.0;
sourceX=0;
sourceY=0;
ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
ALLEGRO_KEYBOARD_STATE StatusKlawiatury1 ; // klawiatura
ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_draw_bitmap(mapa1,0,0,0);
al_start_timer(timer);
while(!done)
{
ALLEGRO_EVENT events;
al_wait_for_event(event_queue, &events);
if(events.type == ALLEGRO_EVENT_KEY_UP)
{
switch(events.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
}
}
else if(events.type == ALLEGRO_EVENT_TIMER)
{
active=true;
al_get_keyboard_state(&StatusKlawiatury1);
if(al_key_down(&StatusKlawiatury1, ALLEGRO_KEY_DOWN))
{
yp += moveSpeed;
dir= DOWN;
}
else if(al_key_down(&StatusKlawiatury1, ALLEGRO_KEY_UP))
{
yp -= moveSpeed;
dir = UP;
}
else if(al_key_down(&StatusKlawiatury1, ALLEGRO_KEY_RIGHT))
{
xp += moveSpeed;
dir= RIGHT;
}
else if(al_key_down(&StatusKlawiatury1, ALLEGRO_KEY_LEFT))
{
xp -= moveSpeed;
dir = LEFT;
}
else
active = false;
Collision(xp,yp,0,426,627,61,dir,moveSpeed);
Collision(xp,yp,700,273,647,53,dir,moveSpeed);
Collision(xp,yp,0,128,695,58,dir,moveSpeed);
Collision(xp,yp,0,0,351,20,dir,moveSpeed);
Collision(xp,yp,770,0,351,20,dir,moveSpeed);
Collision(xp,yp,0,0,0,600,dir,moveSpeed);
Collision(xp,yp,800,0,10,600,dir,moveSpeed);
Collision(xp,yp,0,595,800,10,dir,moveSpeed);
if(active)
sourceX+=al_get_bitmap_width(party)/2;
else
sourceX=20;
if(sourceX >=al_get_bitmap_width(party))
sourceX=0;
draw = true;
}
if(draw)
{
draw=false;
al_convert_mask_to_alpha(party, al_map_rgb(255,0,255));
al_draw_bitmap_region(party,sourceX,dir*al_get_bitmap_height(party)/4,20,20,xp,yp,NULL);
al_flip_display();
al_draw_bitmap(mapa1,0,0,0);
}
}
}
我花了几个小时搜索解决方案。当我在main中执行它们时,这两个函数都没有问题,但是当我将它们移到单独的类时,它开始显示错误。任何帮助都会被贬低。
答案 0 :(得分:2)
函数定义中缺少类限定符Mapa::
。
void Maoa::Collision(float xp,float yp,float ex,float ey,int width,int height,int dir,int moveSpeed)
答案 1 :(得分:1)
错误中的“LNK”部分告诉您编译器成功,问题出在链接器中。 C ++只需要一个函数声明,用于调用所述函数进行编译的代码,但它还需要一个函数定义来链接。
在您的情况下,错误告诉您缺少Maoa::Collision
的定义。实际上,代码中没有该成员的函数定义,只有名为Collision
的全局函数的定义。
您需要从
更改功能定义void Collision(float xp,float yp,float ex,float ey,int width,int height,int dir,int moveSpeed)
到
void Maoa::Collision(float xp,float yp,float ex,float ey,int width,int height,int dir,int moveSpeed)