0

I created base Sprite class:

#ifndef SPRITE_H_
#define SPRITE_H_

#include<iostream>
#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>

class Sprite
{
public:
    Sprite(SDL_Texture* texture, int x, int y, int w, int h, SDL_Renderer* p_renderer);
    virtual ~Sprite();
    SDL_Rect getRect();
    SDL_Texture* loadImage(std::string file_path);
    void drawSprite();
    void setPosition(int x, int y);
    float getXpos();
    float getYpos();
private:
    SDL_Renderer* gRenderer;
    SDL_Texture* image;
    SDL_Rect rect;
    //for position update
    float x_pos;
    float y_pos;
};

#endif /* SPRITE_H_ */

    #include "Sprite.h"

Sprite::Sprite(SDL_Texture* texture, int x, int y, int w, int h, SDL_Renderer* p_renderer)
{
    gRenderer = p_renderer;
    image = texture;
    rect.x = x;
    rect.y = y;
    rect.w = w;
    rect.h = h;
    x_pos = x;
    y_pos = y;
}

Sprite::~Sprite()
{
    SDL_DestroyTexture(image);
}

float Sprite::getXpos()
{
    return x_pos;
}

float Sprite::getYpos()
{
    return y_pos;
}

SDL_Texture* loadImage(std::string file_path, SDL_Renderer* p_renderer)
{
    return IMG_LoadTexture(p_renderer, file_path.c_str());
}

void Sprite::drawSprite()
{
    SDL_RenderCopy(gRenderer, image, NULL, &rect);
}

Next I created Bird class that is using Sprite class as a base:

    /*
 * Bird.h
 *
 *  Created on: 1 maj 2016
 *      Author: Astral
 */

#ifndef BIRD_H_
#define BIRD_H_

#include "Sprite.h"

class Bird: public Sprite
{
public:
    Bird(SDL_Texture* texture, int x, int y, int w, int h, SDL_Renderer* p_renderer, float p_speed, float p_acceleration);
    virtual ~Bird();
    void updateBird(int x, int y);
    void handleInput();
private:
    float speed, acceleration;
};

#endif /* BIRD_H_ */

    #include "Bird.h"



Bird::Bird(SDL_Texture* texture, int x, int y, int w, int h, SDL_Renderer* p_renderer, float p_speed, float p_acceleration):Sprite(texture, x, y, w, h, p_renderer)
{
    speed = p_speed;
    acceleration = p_acceleration;
}

Bird::~Bird()
{

}

void Bird::updateBird(int x, int y)
{
    Sprite::setPosition(x, y);
}

Now I'm getting error and I have no idea why: ../src/Bird.cpp:18: undefined reference to `Sprite::setPosition(int, int)'

  • 1
    Why are you using Sprite:: ? setPosition is not a static method. Why not setPosition ? – perencia May 01 '16 at 10:31
  • `Sprite::setPosition` seems to be missing from the `Sprite.cpp` file; maybe you're missing the function definition (or forgot to include it here). – jpw May 01 '16 at 10:31
  • Your header file tells the compiler it will exist, but when everything is compiled and the compiler (linker actually) goes looking for that function, it's nowhere to be found because you never implemented it. – xaxxon May 01 '16 at 10:33
  • It was missing defi, thanks. – Paweł Kowalski May 01 '16 at 10:36

1 Answers1

0
Sprite.cpp

1.
void Sprite::setPosition(int x, int y)
{
    x_pos = x;
    y_pos = y;
}

2.
SDL_Texture* loadImage(std::string file_path, SDL_Renderer* p_renderer)
 ↓
SDL_Texture* Sprite::loadImage(std::string file_path)
nariuji
  • 288
  • 1
  • 6