我在我的程序/游戏的主要功能中调用的三个函数有问题:initEnemy,drawEnemy和updateEnemy。每个都在运行时附加了这个错误:“错误LNK2019:未解析的外部符号”...“在函数_main中引用”
函数调用在我的main函数中用红色加下划线,当突出显示时说:“错误:多个重载函数实例”......“匹配参数列表:”
如果有人能帮我理解问题所在,我们将不胜感激。谢谢。
#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_image.h>
#include <iostream>
#include <stdio.h>
#include "objects.h"
#include <cstdlib>
#include <random>
using namespace std;
//GLOBALS==============================
const int WINDOW_WIDTH = 1000;
const int WINDOW_HEIGHT = 675;
const int NUM_BULLET = 5;
const int NUM_ENEMY = 5;
enum STATE{TITLE, PLAYING, LOST};
enum KEYS{UP, DOWN, LEFT, RIGHT, SPACE, W, A, S, D};
bool keys[9] = {false, false, false, false, false, false, false, false, false};
int vx = 25;
int shipvy = 15;
int runnervy = 15;
int i;
int middleRect_x1 = 0;
int middleRect_x2 = WINDOW_WIDTH;
float middleRect_y1 = (WINDOW_HEIGHT / 2) - 40;
float middleRect_y2 = (WINDOW_HEIGHT / 2) + 40;
float minJumpThing = (WINDOW_HEIGHT - middleRect_y2) / 2;
int playerScore = 0;
int playerLives = 0;
//function prototypes
void initRunner(Runner &runner);
void minimumGravRunner(Runner &runner);
void drawRunner(Runner &runner);
void runnerJump(Runner &runner);
void runnerFall(Runner &runner);
void stopRunner(Runner &runner);
void initShip(Ship &ship);
void drawShip(Ship &ship);
void moveShipUp(Ship &ship);
void moveShipDown(Ship &ship);
void stopShipBrick(Ship &ship);
void stopShipWindow(Ship &ship);
void initBullet(Bullet bullet[], int bulletSize);
void drawBullet(Bullet bullet[], int bulletSize);
void fireBullet(Bullet bullet[], int bulletSize, Ship &ship);
void updateBullet(Bullet bullet[], int bulletSize);
void initEnemy(Enemy &enemy, int enemySize);
void drawEnemy(Enemy &enemy);
void updateEnemy(Bullet bullet[], int enemySize, Enemy &enemy);
int main(void)
{
//primitive variable
bool done = false;
bool redraw = true;
float gameTime = 0;
int frames = 0;
int gameFPS = 0;
Ship ship;
Runner runner;
Bullet bullet[5];
Enemy enemy;
//object variables
//Allegro variables
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_FONT *arial = NULL;
ALLEGRO_FONT *dosapp = NULL;
//Initialization Functions
if(!al_init()) //initialize Allegro
return -1;
display = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT); //create our display object
if(!display) //test display object
return -1;
al_init_primitives_addon();
al_install_keyboard();
al_init_font_addon();
al_init_ttf_addon();
al_init_image_addon();
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / 60);
arial = al_load_font("arial.ttf", 18, 0);
dosapp = al_load_font("dosapp.fon", 40, 0);
initBullet(bullet,NUM_BULLET);
initRunner(runner);
initShip(ship);
initEnemy(enemy, NUM_ENEMY);
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_display_event_source(display));
al_start_timer(timer);
gameTime = al_current_time();
while(!done)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_TIMER)
{
if(keys[UP])
{
runnerJump(runner);
}
if(runner.y1 <= middleRect_y2 - 10)
{
runnerFall(runner);
keys[UP] = false;
}
if(!keys[UP])
{
if(runner.y1 < (WINDOW_HEIGHT - 40))
runnerFall(runner);
else if(runner.y1 >= runner.minimumJump - 40 && runner.y2 >= runner.minimumJump &&
runner.y1 != (WINDOW_HEIGHT - 40) && runner.y2
!= WINDOW_HEIGHT)
{
keys[UP] = true;
runnerJump(runner);
}
}
if(keys[W])
moveShipUp(ship);
if(keys[S])
moveShipDown(ship);
if(ship.y1 <= 0)
stopShipWindow(ship);
else if(ship.y2 >= middleRect_y1)
stopShipBrick(ship);
if(keys[SPACE])
drawBullet(bullet,NUM_BULLET);
updateBullet(bullet,NUM_BULLET);
updateEnemy(bullet,NUM_ENEMY,enemy);
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}
else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_UP:
if(runner.y1 == (WINDOW_HEIGHT - 40) && runner.y2 == WINDOW_HEIGHT)
keys[UP] = true;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = true;
break;
case ALLEGRO_KEY_W:
keys[W] = true;
break;
case ALLEGRO_KEY_S:
keys[S] = true;
break;
case ALLEGRO_KEY_SPACE:
if(!bullet[i].live)
{
keys[SPACE] = true;
fireBullet(bullet, NUM_BULLET, ship);
}
break;
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_UP:
//if(runner.y1 <= runner.minimumJump - 40 && runner.y2 <= runner.minimumJump)
keys[UP] = false;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;
case ALLEGRO_KEY_W:
keys[W] = false;
break;
case ALLEGRO_KEY_S:
keys[S] = false;
break;
case ALLEGRO_KEY_SPACE:
keys[SPACE] = false;
break;
}
}
if(redraw && al_is_event_queue_empty(event_queue))
{
al_draw_filled_rectangle(middleRect_x1, middleRect_y1, middleRect_x2, middleRect_y2, al_map_rgb(255,255,255));
al_draw_textf(arial,al_map_rgb(0,0,0),WINDOW_WIDTH / 2, (WINDOW_HEIGHT / 2) - 20,ALLEGRO_ALIGN_CENTRE,"Score: %d",
playerScore);
al_draw_textf(arial,al_map_rgb(0,0,0),WINDOW_WIDTH / 2, (WINDOW_HEIGHT / 2) + 13,ALLEGRO_ALIGN_CENTRE,"Lives: %d",
playerLives);
drawShip(ship);
drawRunner(runner);
drawBullet(bullet,NUM_BULLET);
if(enemy.live = true)
drawEnemy(enemy);
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
}
al_destroy_event_queue(event_queue);
al_destroy_timer(timer);
al_destroy_font(arial);
al_destroy_font(dosapp);
al_destroy_display(display); //destroy our display object
return 0;
}
void initRunner(Runner &runner)
{
runner.ID = RUNNER;
runner.x1 = 10;
runner.x2 = runner.x1 + 40;
runner.y1 = WINDOW_HEIGHT - 40;
runner.y2 = runner.y1 + 40;
runner.minimumJump = (middleRect_y2 + minJumpThing) + 45;
}
void drawRunner(Runner &runner)
{
al_draw_filled_rectangle(runner.x1,runner.y1,runner.x2,runner.y2, al_map_rgb(255,255,255));
}
void runnerJump(Runner &runner)
{
runner.y1 -= runnervy;
runner.y2 -= runnervy;
}
void runnerFall(Runner &runner)
{
runner.y1 += runnervy;
runner.y2 += runnervy;
}
void stopRunner(Runner &runner)
{
runner.y1 = (WINDOW_HEIGHT - 40);
runner.y2 = WINDOW_HEIGHT;
}
void initShip(Ship &ship)
{
ship.ID = SHIP;
ship.x1 = 10;
ship.x2 = ship.x1;
ship.x3 = ship.x1 + 40;
ship.y1 = (WINDOW_HEIGHT / 2) / 4 + 50;
ship.y2 = ship.y1 + 40;
ship.y3 = ship.y1 + 20;
}
void drawShip(Ship &ship)
{
al_draw_filled_triangle(ship.x1,ship.y1,ship.x2,ship.y2,ship.x3,ship.y3,al_map_rgb(255,255,255));
}
void moveShipUp(Ship &ship)
{
ship.y1 -= shipvy;
ship.y2 -= shipvy;
ship.y3 -= shipvy;
}
void moveShipDown(Ship &ship)
{
ship.y1 += shipvy;
ship.y2 += shipvy;
ship.y3 += shipvy;
}
void stopShipWindow(Ship &ship)
{
ship.y1 = 0;
ship.y2 = 40;
ship.y3 = 20;
}
void stopShipBrick(Ship &ship)
{
ship.y1 = middleRect_y1 - 40;
ship.y2 = middleRect_y1;
ship.y3 = middleRect_y1 - 20;
}
void initBullet(Bullet bullet[], int bulletSize)
{
for(int i = 0; i < bulletSize; i++)
{
bullet[i].ID = BULLET;
bullet[i].speed = 20;
bullet[i].live = false;
keys[SPACE] = false;
}
}
void drawBullet(Bullet bullet[], int bulletSize)
{
for(int i = 0; i < bulletSize; i++)
{
if(bullet[i].live)
{
al_draw_filled_circle(bullet[i].x,bullet[i].y,2,al_map_rgb(255,255,255));
keys[SPACE] = false;
}
}
}
void fireBullet(Bullet bullet[], int bulletSize, Ship &ship)
{
for(int i = 0; i < bulletSize; i++)
{
if(!bullet[i].live)
{
bullet[i].x = ship.x3;
bullet[i].y = ship.y3;
bullet[i].live = true;
keys[SPACE] = false;
break;
}
}
}
void updateBullet(Bullet bullet[], int bulletSize)
{
for(int i = 0; i < bulletSize; i++)
{
if(bullet[i].live)
{
bullet[i].x += bullet[i].speed;
if(bullet[i].x >= WINDOW_WIDTH)
bullet[i].live = false;
}
}
}
void initEnemy(Enemy enemy, int enemySize)
{
for(int i = 0; i < enemySize; i++)
{
std::default_random_engine y1_generator;
std::uniform_int_distribution<int> y1_distribution(0,middleRect_y1);
enemy.ID = ENEMY;
enemy.live = true;
enemy.x1 = WINDOW_WIDTH + 50;
enemy.x2 = enemy.x1 + 40;
enemy.y1 = y1_distribution(y1_generator);
enemy.y2 = enemy.y1 + 40;
enemy.speed = 20;
}
}
void drawEnemy(Enemy enemy)
{
al_draw_filled_rectangle(enemy.x1,enemy.y1,enemy.x2,enemy.y2,al_map_rgb(255,0,0));
}
void updateEnemy(Bullet bullet[], int enemySize, Enemy enemy)
{
for(int i = 0; i < enemySize; i++)
{
if(enemy.live)
{
std::default_random_engine y1_generator;
std::uniform_int_distribution<int> y1_distribution(0,middleRect_y1);
enemy.x1 = WINDOW_WIDTH + 50;
enemy.x2 = enemy.x1 + 40;
enemy.y1 = y1_distribution(y1_generator);
enemy.y2 = enemy.y1 + 40;
enemy.x1 -= enemy.speed;
enemy.x2 -= enemy.speed;
if(bullet[i].x == enemy.x1 && bullet[i].x == enemy.x2 - 40 && bullet[i].y >= enemy.y1 && bullet[i].y <= enemy.y2)
enemy.live = false;
}
}
}
答案 0 :(得分:2)
这种情况正在发生,因为正如@DCoder所说,你转发声明具有略微不同签名的函数作为你的定义。这导致编译器假设它们是两个不同的函数,因此就是错误。
更改以下定义以进行修复。 (基本上将所有&
符号添加到您错过的Enemy
个变量中
void initEnemy(Enemy& enemy, int enemySize)
{
void drawEnemy(Enemy& enemy)
{
void updateEnemy(Bullet bullet[], int enemySize, Enemy& enemy)
{