0

The error which comes every time is

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\msys64\tmp\ccmtnC4W.o:C:\Users\param\Documents\code\7.2/program.cpp:42: undefined reference to `draw_hud(player_data const&, planet_data const&)'
collect2.exe: error: ld returned 1 exit status

Is there any method of solving it?

#include "splashkit.h"
#include "player.h"
#include "planet.h"

/**
 * Load the game images, sounds, etc.
 */
void load_resources()
{
    load_resource_bundle("game_bundle", "lost_in_space.txt");
}

/**
 * Entry point.
 * 
 * Manages the initialisation of data, the event loop, and quitting.
 */
int main()
{
    open_window("Lost In Space", 800, 800);
    load_resources();

    player_data player;
    player = new_player();

    planet_data planet;
    planet = new_planet(200, 300);

    while ( not quit_requested() )
    {
        // Handle input to adjust player movement
        process_events();
        handle_input(player);

        // Perform movement and update the camera
        update_player(player);
        update_planet(planet);

        // Redraw everything
        clear_screen(COLOR_BLACK);

        draw_hud(player, planet); //THIS IS THE ERROR LINE

        // as well as the player who can move
        draw_planet(planet);
        draw_player(player);

        refresh_screen(60);

    }

    return 0;
}

//There is another file as player.h in which this is defined. There are other files also but the problem is due to these two only

#ifndef LOST_IN_SPACE_PLAYER
#define LOST_IN_SPACE_PLAYER

#include "splashkit.h"
#include "planet.h"
#include <vector>

using namespace std;

#define PLAYER_SPEED 1.5
#define PLAYER_ROTATE_SPEED 3
#define SCREEN_BORDER 100

/**
 * Different options for the kind of ship.
 * Adjusts the image used.
 */
enum ship_kind
{
    AQUARII,
    GLIESE,
    PEGASI
};

/**
 * The player data keeps track of all of the information related to the player.
 * 
 * @field   player_sprite   The player's sprite - used to track position and movement
 * @field   score           The current score for the player
 * @field   kind            Current kind of player ship
 */
struct player_data
{
    sprite      player_sprite; //Character element
    int         score = 0;
    ship_kind   kind;
};

/**
 * Creates a new player in the centre of the screen with the default ship.
 * 
 * @returns     The new player data
 */
player_data new_player();

/**
 * Draws the player to the screen. 
 * 
 * @param player_to_draw    The player to draw to the screen
 */
void draw_player(const player_data &player_to_draw);

/**
 * Actions a step update of the player - moving them and adjusting the camera.
 * 
 * @param player_to_update      The player being updated
 */
void update_player(player_data &player_to_update);

/**
 * Read user input and update the player based on this interaction.
 * 
 * @param player    The player to update
 */
void handle_input(player_data &player);

//draw the location, score and the process bar
void draw_hud(const player_data &player, const planet_data &planet); //THIS IS THE ERROR LINE

#endif 

//player.cpp file added This is the player.cpp file in which the draw_hud is defined. you can check that and suggest me what is wrong and how to solve the problem

#include "player.h"
#include "splashkit.h"
#include <string>

using namespace std;

/**
 * The ship bitmap function converts a ship kind into a 
 * bitmap that can be used.
 * 
 * Not exposed by the header.
 * 
 * @param kind  The kind of ship you want the bitmap of
 * @return      The bitmap matching this ship kind
 */
bitmap ship_bitmap(ship_kind kind)
{
    switch (kind)
    {
    case AQUARII:
        return bitmap_named("aquarii");
    case GLIESE:
        return bitmap_named("gliese");
    default:
        return bitmap_named("pegasi");
    }
}

player_data new_player()
{
    player_data result;
    bitmap default_bitmap = ship_bitmap(AQUARII);

    // Create the sprite with 3 layers - we can turn on and off based
    // on the ship kind selected
    result.player_sprite = create_sprite(default_bitmap);

    sprite_add_layer(result.player_sprite, ship_bitmap(GLIESE), "GLIESE");
    sprite_add_layer(result.player_sprite, ship_bitmap(PEGASI), "PEGASI");

    // Default to layer 0 = Aquarii so hide others
    sprite_hide_layer(result.player_sprite, 1);
    sprite_hide_layer(result.player_sprite, 2);

    result.kind = AQUARII;

    // Position in the centre of the initial screen
    sprite_set_x(result.player_sprite, ( screen_width() - sprite_width(result.player_sprite) )/ 2);
    sprite_set_y(result.player_sprite, ( screen_height() - sprite_height(result.player_sprite) )/ 2);

    return result;
}

void draw_player(const player_data &player_to_draw)
{
    draw_sprite(player_to_draw.player_sprite);
}

void update_player(player_data &player_to_update)
{
    // Apply movement based on rotation and velocity in the sprite
    update_sprite(player_to_update.player_sprite);

    // Test edge of screen boundaries to adjust the camera
    double left_edge = camera_x() + SCREEN_BORDER;
    double right_edge = left_edge + screen_width() - 2 * SCREEN_BORDER;
    double top_edge = camera_y() + SCREEN_BORDER;
    double bottom_edge = top_edge + screen_height() - 2 * SCREEN_BORDER;

    // Get the center of the player
    point_2d sprite_center = center_point(player_to_update.player_sprite);

    // Test if the player is outside the area and move the camera
    // the player will appear to stay still and everything else
    // will appear to move :)

    // Test top/bottom of screen
    if (sprite_center.y < top_edge)
    {
        move_camera_by(0, sprite_center.y - top_edge);
    }
    else if (sprite_center.y > bottom_edge)
    {
        move_camera_by(0, sprite_center.y - bottom_edge);
    }

    // Test left/right of screen
    if (sprite_center.x < left_edge)
    {
        move_camera_by(sprite_center.x - left_edge, 0);
    }
    else if (sprite_center.x > right_edge)
    {
        move_camera_by(sprite_center.x - right_edge, 0);
    }
}

/**
 * Switch the ship kind - will adjust which layer is hidden/shown
 */
void player_switch_to_ship(player_data &player, ship_kind target)
{
    // only do this if there is a change
    if (player.kind != target)
    {
        // show then hide layers
        sprite_show_layer(player.player_sprite, static_cast<int>(target));
        sprite_hide_layer(player.player_sprite, static_cast<int>(player.kind));

        // remember what is currently shown
        player.kind = target;
    }
}

void handle_input(player_data &player)
{
    // Allow the player to switch ships
    if (key_typed(NUM_1_KEY))
        player_switch_to_ship(player, AQUARII);
    if (key_typed(NUM_2_KEY))
        player_switch_to_ship(player, GLIESE);
    if (key_typed(NUM_3_KEY))
        player_switch_to_ship(player, PEGASI);

    // Handle movement - rotating left/right and moving forward/back
    float dx = sprite_dx(player.player_sprite);
    float rotation = sprite_rotation(player.player_sprite);

    // Allow rotation with left/right keys
    if (key_down(LEFT_KEY))
        sprite_set_rotation(player.player_sprite, rotation - PLAYER_ROTATE_SPEED);
    if (key_down(RIGHT_KEY))
        sprite_set_rotation(player.player_sprite, rotation + PLAYER_ROTATE_SPEED);

    // Increase speed with up/down keys - typed to give step increases
    if (key_typed(DOWN_KEY))
        sprite_set_dx(player.player_sprite, dx - PLAYER_SPEED);
    if (key_typed(UP_KEY))
        sprite_set_dx(player.player_sprite, dx + PLAYER_SPEED);
}

void draw_hud(player_data &player, planet_data &planet)
{
    //integer variables that store player's location
    int x, y, distance;
    //struct that stores player's location and planet's location
    point_2d player_location, planet_location;
    
    //assign the center point of each sprite to its struct
    player_location = center_point(player.player_sprite);
    planet_location = center_point(planet.planet_sprite);

    //asign the x, y location to the integer variables
    x = player_location.x;
    y = player_location.y;

    //expresion to calculate distance between the player's sprite and planet's sprite
    distance = point_point_distance(player_location, planet_location);

    //draw the head up display with shape background, location, score, distance and progress bar
    fill_rectangle(COLOR_DARK_GRAY,0 ,0 , 320, 92, option_to_screen());
    draw_rectangle(COLOR_WHITE,0 ,0 , 320, 92, option_to_screen());
    draw_text("LOCATION: " + to_string(x) + "," + to_string(y), COLOR_WHITE, 10, 10, option_to_screen());
    draw_text("SCORE: ", COLOR_WHITE, 10, 20, option_to_screen());
    draw_text("DISTANCE: " + to_string(distance), COLOR_WHITE, 10, 30, option_to_screen());
    draw_bitmap(bitmap_named("empty_bar_transparent"), 10, 40, option_to_screen());
    draw_bitmap(bitmap_named("purple_bar"), 10, 40, option_part_bmp(0, 0, 150, 42, option_to_screen()));
}
faliure1
  • 13
  • 4
  • 1
    Where is the implementation for the `draw_hud` function? – fabian Jan 23 '21 at 08:36
  • @fabian In the first code where i have written //THIS IS THE ERROR LINE – faliure1 Jan 23 '21 at 08:40
  • 1
    The compiler is telling you that you haven't **defined** the `draw_hud` function. In the code you have quoted there is a **declaration** for this function but no definition. Do you think you have defined this function? If so then please show the definition. If you have not defined this function then you must define it. If you are unsure what a function definition is, or what the difference between a function declaration and a function definition is then you need to go back to your C++ text book. – john Jan 23 '21 at 08:41
  • @faliure1 `//THIS IS THE ERROR LINE` As I said this shows the function **declaration**, the error is about the missing **definition**. – john Jan 23 '21 at 08:43
  • @faliure1 Somewhere there should be a .cpp file with the function definition, that's the code we need to see (assuming it exists). – john Jan 23 '21 at 08:45
  • @john i have aadded the .cpp file. please check now – faliure1 Jan 23 '21 at 09:03
  • @faliure1 OK, well look at your declaration `void draw_hud(const player_data &player, const planet_data &planet);` and now look at your definition `void draw_hud(player_data &player, planet_data &planet) { ... }`. The declaration has `const` but the definition does not. Everything should match up, either remove const from the declaration or add const to the definition (I can't immediately say which is correct) – john Jan 23 '21 at 09:09

0 Answers0