Reputation: 47
I have some class called gSprite that allows easy manipulation of image , manipulation and etc. As I completed some of it's functions and fixed all errors, I tried to use it and got others. Here is class custom creators
gSprite * crGSprite(sf::Sprite * sprsource){
return new gSprite(sprsource);
}
gSprite * crGSprite(sf::Sprite * sprsource,int sx, int sy){
return new gSprite(sprsource,sx,sy);
}
gSprite * crGSprite(std::vector<sf::Sprite> spr,int sx,int sy){
return new gSprite(spr,sx,sy);
}
All creators are just construct new object and return reference to it. Then I have function for storing data about all files and create them in other file, that can reach these creators:
gSprite * loadsprite(string path)
{
sf::Sprite sprite;
sf::Texture tex;
tex.loadFromFile(path);
sprite.setTexture(tex);
gSprite * gspr = crGSprite(&sprite);
addGSprite(gspr);
return gspr;
}
And all fine. BUT. Class also contains some functions.For some reason when I try to execute update(), it works, but when I trying to execute setPosition() it fails with "undefined reference to gSprite::setPosition(int,int)
.
Here is these two functions and .hpp file:
void update()
{
sprite = anim_sprites[num];
sprite.setPosition(sf::Vector2f(x,y));
return;
}
and
void setPosition(int nx,int ny)
{
x=nx;
y=ny;
return;
}
hpp:
#ifndef Sprites_H_
#define Sprites_H_
extern sf::RenderWindow * windowsrc;
class gSprite {
int x;
int y;
sf::Sprite sprite;
std::vector<sf::Sprite> anim_sprites;
sf::Vector2f size;
bool anim;
int num;
public:
gSprite(sf::Sprite * sprsource);
gSprite(sf::Sprite * sprsource,int sx, int sy);
gSprite(std::vector<sf::Sprite> spr,int sx,int sy);
void setSize(float x, float y);
void resize(float x,float y);
void scale(float x,float y);
void draw_s();
void update();
void move(int nx,int ny);
void setPosition(int nx,int ny);
};
gSprite * crGSprite(sf::Sprite * sprsource);
gSprite * crGSprite(sf::Sprite * sprsource,int sx, int sy);
gSprite * crGSprite(std::vector<sf::Sprite> spr,int sx,int sy);
void addGSprite(gSprite * sprtsource);
void drawGSprites();
#endif /* Sprites_H_ */
I declare methods inside class declaration
Upvotes: 0
Views: 919
Reputation: 34591
When you write
void setPosition(int nx,int ny)
{
x=nx;
y=ny;
return;
}
you're defining a standalone function that's not part of any class. To implement the member function declared in the gSprite
class, you have to write
void gSprite::setPosition(int nx,int ny)
{
x=nx;
y=ny;
return;
}
Upvotes: 2