2016-05-01 14 views
0

は、私は基本Spriteクラスを作成しました:未定義の参照の継承

#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); 
} 

次私はベースとして、Spriteクラスを使用している鳥のクラスを作成しました:

/* 
* 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); 
} 

は今、私はエラーを取得していると私はありません持っていますアイデア理由: ../src/Bird.cpp:18:なぜあなたはスプライトを使用している未定義のスプライト `への参照:: setPosition(int型、int型)」

+1

: :? setPositionは静的メソッドではありません。なぜsetPositionはできませんか? – perencia

+0

'Sprite :: setPosition'が' Sprite.cpp'ファイルから抜けているようです。多分あなたは関数の定義を見落としているかもしれません(または、ここにそれを忘れています)。 – jpw

+0

あなたのヘッダーファイルはコンパイラに存在することをコンパイラに伝えますが、すべてがコンパイルされコンパイラ(実際にはリンカ)がその関数を探していくと、決して実装されていないため、どこにも見つかりません。 – xaxxon

答えて

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)