Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Calling constructors from inherited classes  (Read 4705 times)

0 Members and 1 Guest are viewing this topic.

booncoder

  • Newbie
  • *
  • Posts: 9
    • View Profile
Calling constructors from inherited classes
« 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.
Code: [Select]

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

Code: [Select]

#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
Code: [Select]

#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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Calling constructors from inherited classes
« Reply #1 on: April 15, 2010, 06:54:50 pm »
Can you show the cpp file of Obstacle?
Laurent Gomila - SFML developer

booncoder

  • Newbie
  • *
  • Posts: 9
    • View Profile
Calling constructors from inherited classes
« Reply #2 on: April 15, 2010, 06:56:59 pm »
here it is

Code: [Select]

#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()
{
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Calling constructors from inherited classes
« Reply #3 on: April 15, 2010, 07:06:52 pm »
What is the exact error message?
Laurent Gomila - SFML developer

booncoder

  • Newbie
  • *
  • Posts: 9
    • View Profile
Calling constructors from inherited classes
« Reply #4 on: April 15, 2010, 07:09:24 pm »
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

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Calling constructors from inherited classes
« Reply #5 on: April 15, 2010, 07:20:13 pm »
Code: [Select]
sinkingObstacle::sinkingObstacle(const Obstacle &Obs) :
Obstacle(Img,Postion,Scale,Roation,Col,scrollPos)
{
}


You mixed your overloaded constructor with your copy constructor...

Should be
Code: [Select]
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

Code: [Select]
sinkingObstacle::sinkingObstacle(const Obstacle &Obs) :
Obstacle(Obs)
{
}


wich can also be

Code: [Select]
sinkingObstacle::sinkingObstacle(const sinkingObstacle &Obs) :
Obstacle(Obs)
{
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Calling constructors from inherited classes
« Reply #6 on: April 15, 2010, 07:22:15 pm »
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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

booncoder

  • Newbie
  • *
  • Posts: 9
    • View Profile
Calling constructors from inherited classes
« Reply #7 on: April 15, 2010, 07:23:41 pm »
ahh didn't notice thanks yea the headers where in there. thanks for the tip nexus

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Calling constructors from inherited classes
« Reply #8 on: April 16, 2010, 09:42:30 am »
I really think this code is strange :

Code: [Select]
lass Obstacle:public Sprite
{  
private:
   sf::Sprite explosion;

Why using a Sprite as a member while using it for inheritance ?
Mindiell
----

booncoder

  • Newbie
  • *
  • Posts: 9
    • View Profile
Calling constructors from inherited classes
« Reply #9 on: April 19, 2010, 06:12:12 pm »
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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Calling constructors from inherited classes
« Reply #10 on: April 19, 2010, 07:16:26 pm »
Can you show the current code?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

booncoder

  • Newbie
  • *
  • Posts: 9
    • View Profile
Calling constructors from inherited classes
« Reply #11 on: April 19, 2010, 08:00:36 pm »
its basicly the same from previous jsut constructor changes

sinking cpp
Code: [Select]

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
Code: [Select]

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
Code: [Select]

#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

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Calling constructors from inherited classes
« Reply #12 on: April 20, 2010, 09:08:31 am »
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.
Mindiell
----

 

anything