SFML community forums
Help => General => Topic started by: booncoder on April 15, 2010, 06:21:39 pm
-
hi my problem is that i can't draw my sprite using during the game loop because it doesn't inherit the constructors of other classes and when im trying to call the constructor of another class it doesn't like it.
class Obstacle:public Sprite
{
private:
float scrollposition;
float verticalposition;
int cost;
sf::Image img;
sf::Sprite explosion;
public:
Obstacle(const Image &Img, const Vector2f &Position=Vector2f(0, 0), const Vector2f &Scale=Vector2f(1, 1), float Rotation=0.f, const Color &Col=Color(255, 255, 255, 255),float scrollPos=0.0);
Obstacle(const Obstacle &Obs);
only a bit of the class. takes the sprite constructor and calls it in a different cpp
#include "obstacle.h"
//inherits obstacle methods but not constructors
class sinkingObstacle: public Obstacle
{
private:
int cost;
public:
sinkingObstacle();
sinkingObstacle(const Obstacle &Obs);
int getcost();
void setcost(int c);
//virtual void Render (const RenderWindow &Window) const ;
};
cpp file
#include "sinkingObstacle.h"
#include <SFML/Graphics.hpp>
using namespace sf;
sinkingObstacle::sinkingObstacle(const Obstacle &Obs):Obstacle(Img,Postion,Scale,Roation,Col,scrollPos)
{
}
sinkingObstacle::sinkingObstacle():Obstacle()
{
}
says obstacle(Img,Postion,Scale,Roation,Col,scrollpos) not defined even though i used the sprite constructor in the same way.
-
Can you show the cpp file of Obstacle?
-
here it is
#include "obstacle.h"
Obstacle::Obstacle(const Image &Img, const Vector2f &Position, const Vector2f &Scale, float Rotation, const Color &Col, float scrollPos):Sprite (Img ,Position, Scale, Rotation, Col)
{
}
Obstacle::Obstacle(const Obstacle &Obs)
{
}
Obstacle::Obstacle():Sprite()
{
}
-
What is the exact error message?
-
sinkingobstacle.cpp(5) : error C2065: 'Img' : undeclared identifier
sinkingobstacle.cpp(5) : error C2065: 'Postion' : undeclared identifier
sinkingobstacle.cpp(5) : error C3867: 'sf::Drawable::Scale': function call missing argument list; use '&sf::Drawable::Scale' to create a pointer to member
sinkingobstacle.cpp(5) : error C2065: 'Roation' : undeclared identifier
sinkingobstacle.cpp(5) : error C2065: 'Col' : undeclared identifier
sinkingobstacle.cpp(5) : error C2065: 'scrollPos' : undeclared identifier
-
sinkingObstacle::sinkingObstacle(const Obstacle &Obs) :
Obstacle(Img,Postion,Scale,Roation,Col,scrollPos)
{
}
You mixed your overloaded constructor with your copy constructor...
Should be
sinkingObstacle::sinkingObstacle(const Image &Img, const Vector2f &Position, const Vector2f &Scale, float Rotation, const Color &Col, float scrollPos) :
Obstacle(Img,Postion,Scale,Roation,Col,scrollPos)
{
}
or
sinkingObstacle::sinkingObstacle(const Obstacle &Obs) :
Obstacle(Obs)
{
}
wich can also be
sinkingObstacle::sinkingObstacle(const sinkingObstacle &Obs) :
Obstacle(Obs)
{
}
-
You should include the required header files.
By the way, never write using namespace in headers, because this irreversibly opens the namespace for all including files.
-
ahh didn't notice thanks yea the headers where in there. thanks for the tip nexus
-
I really think this code is strange :
lass Obstacle:public Sprite
{
private:
sf::Sprite explosion;
Why using a Sprite as a member while using it for inheritance ?
-
yea copy and paste issue thanks.
anyway got another strange error i changed my constructor calls and they work but i did have an overloaded constructor and i deleted it but it still says it is there.
1>mainscroller.cpp(57) : error C2664: 'sinkingObstacle::sinkingObstacle(const sinkingObstacle &)' : cannot convert parameter 1 from 'sf::Image' to 'const sinkingObstacle &'
1> Reason: cannot convert from 'sf::Image' to 'const sinkingObstacle'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
even though it is using the copy constructor
-
Can you show the current code?
-
its basicly the same from previous jsut constructor changes
sinking cpp
sinkingObstacle::sinkingObstacle(const Image &Img, const Vector2f &Position, const Vector2f &Scale, float Rotation, const Color &Col, float scrollPos):Obstacle(Img,Position,Scale,Rotation,Col,scrollPos)
{
std::cout<<getScrollPos()<<std::endl;
}
sinkingObstacle::sinkingObstacle():Obstacle()
{
}
obstacle cpp
Obstacle::Obstacle(const Image &Img, const Vector2f &Position, const Vector2f &Scale, float Rotation, const Color &Col, float scrollPos):Sprite (Img ,Position, Scale, Rotation, Col)//,scrollposition(scrollPos)
{scrollposition=scrollPos;
}
Obstacle::Obstacle():Sprite()
{
}
error
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <sstream>
#include "obstacle.h"
#include "explosiveObstacle.h"
#include "sinkingObstacle.h"
#include "Scroller.h"
#include "AnimatedSprite.h"
#include "game.h"
#include "submarine.h"
#include <cmath>
#include <time.h>
int main()
{
//initialisation - lots of this code could be put into functions for
//neatness and separation of concerns. You need to declare objects here to draw them
//but the set up for those objects could be done in a function or a class
//window dimensions
unsigned int width=600;
unsigned int height=480;
Game newgame(0,0);
//we need a timer to update things - get a clock
sf::Clock clock;
static float timeAccumulator=0.0;
//load resources for the scroller - images and sounds (if you have any)
sf::Image backGround;
if(!backGround.LoadFromFile("images/underwater.jpg")) std::cout<<"File not found background.jpg"<<std::endl;
sf::Image Rock;
if(!Rock.LoadFromFile("images/rock.png")) std::cout<<"File not found SpriteRock.png"<<std::endl;
//sf::Sprite SpriteRock;
sf::Image seamine;
if(!seamine.LoadFromFile("images/seamine.png")) std::cout<<"File not found seamine.png"<<std::endl;
//sf::Sprite SpriteSeamine;
sf::Image shipreck;
if(!shipreck.LoadFromFile("images/shipreck.png")) std::cout<<"File not found shipreck.png"<<std::endl;
//sf::Sprite SpriteShipreck;
Submarine newsub(100);
newsub.Init("images/submarine.png");
newsub.getEnergy();
float random = sf::Randomizer::Random(0.f,1.f);
sf::Sprite backGndSprite(backGround);
backGndSprite.Scale(0.75f,0.8f);
//declare obstacles here
//Obstacle ObsRock(Rock);
//set up the scroller data
//sinkingObstacle Seamine(seamine);
sinkingObstacle Seamine(seamine);//error here
my main game loop
-
The compiler is looking for a copy constructor. You don't call your new constructor, because the methd is waiting... 6 variables and you give it only one (the sf::Image).
The default copy constructor is called instead, and sf::Image is not a sinkingObstacle object : Error.
You must either change your constructor with default values or call it with the 6 values.