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

Author Topic: inheritance problem  (Read 4421 times)

0 Members and 1 Guest are viewing this topic.

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
inheritance problem
« on: February 05, 2010, 06:00:39 pm »
PLEASE LOOK AT MY NEXT POST. THIS POST IS NOT UP TO DATE.

Sorry Guys. This is more like a C++ beginners question but maybe you could help me out. Im trying to build a class with inheritance. I read a few online tutorials about it but i dont get the point.
I tried to draw a circle with the class sf::Shape but my compiler keeps screaming "undefined reference to `Asteroid::Asteroid()'"

So here is the code:
Code: [Select]

#ifndef _ASTEROID_H
#define _ASTEROID_H
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

class Asteroid : public sf::Shape {
private:

public:
    Asteroid();
   
    Asteroid(const Asteroid& orig);
    virtual ~Asteroid();


};

#endif /* _ASTEROID_H */

Code: [Select]
#include "Asteroid.h"
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>


double radiAsteroid = 50;

Asteroid::Asteroid() {
    sf::Shape::Circle(400, 400, radiAsteroid, sf::Color(255, 255, 255));

}

Asteroid::Asteroid(const Asteroid& orig) {
   
}

Asteroid::~Asteroid() {
}


main.cpp
Code: [Select]
Asteroid asteroidclass;
Game.Draw(asteroidclass);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
inheritance problem
« Reply #1 on: February 05, 2010, 06:16:16 pm »
You probably forgot to add Asteroid.cpp to your project, so it is not linked to the final executable.
Laurent Gomila - SFML developer

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
inheritance problem
« Reply #2 on: February 05, 2010, 06:19:16 pm »
What tutorials are you using? I think you need to understand classes before starting on inheritance. In your constructor you are calling sf::Shape::Circle but you are not storing it anywhere. Take a look at the function Circle, it returns a Shape and you dont store it anywhere inside of the class.
Eugene Alfonso
GTP | Twitter

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
inheritance problem
« Reply #3 on: February 09, 2010, 05:40:12 pm »
Thanks for your help to this point, but im still stuck with using the sf::Shape class in a selfmade class. I think i understood the fundemantals of using classes and inheritance but somehow im trying for two days now and still cant solve my problem. So help would be very much appreciated

Here is the code:

Asteroid.hpp
Code: [Select]

// Creation of an Asteroid

#ifndef _ASTEROID_HPP
#define _ASTEROID_HPP
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>


class Asteroid : public sf::Shape {

private:
    float X;
    float Y;
    float Radius;
    const sf::Color& Col;
    float Outline;
    const sf::Color& OutlineCol;

public:

    Asteroid(float X, float Y, float Radius, const Color& Col, float Outline = 0.f, const Color& OutlineCol = sf::Color(0, 0, 0))     <---- LINE 22
            : sf::Shape::Circle(X, Y, Radius, Col, Outline, OutlineCol)   {}    <---- LINE 23

    Asteroid(const Asteroid& orig);
    virtual ~Asteroid();


};

#endif // _ASTEROID_HPP


Asteroid.cpp
Code: [Select]

#include "Asteroid.hpp"
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics/Drawable.hpp>


Asteroid::Asteroid(float X, float Y, float Radius, const sf::Color& Col, float Outline, const sf::Color& OutlineCol)
{
   sf::Shape::Circle(X, Y, Radius, Col, Outline, OutlineCol);
}

Asteroid::Asteroid(const Asteroid& orig) {
   
}

Asteroid::~Asteroid() {
}


Here is what the compiler has to say:
In file included from main.cpp:9:
Asteroid.hpp:22: error: expected `,' or `...' before '&' token
Asteroid.hpp:23: error: ISO C++ forbids declaration of `Color' with no type
Asteroid.hpp: In constructor `Asteroid::Asteroid(float, float, float, int)':
Asteroid.hpp:23: error: expected class-name before '(' token
Asteroid.hpp:23: error: uninitialized reference member `Asteroid::Col'
Asteroid.hpp:23: error: uninitialized reference member `Asteroid::OutlineCol'
main.cpp: In function `int main(int, char**)':
main.cpp:131: error: no matching function for call to `Asteroid::Asteroid(float, float, float, sf::Color, float, sf::Color)'
Asteroid.hpp:25: note: candidates are: Asteroid::Asteroid(const Asteroid&)
Asteroid.hpp:23: note:                 Asteroid::Asteroid(float, float, float, int)
[/code]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
inheritance problem
« Reply #4 on: February 09, 2010, 05:48:13 pm »
Quote
Asteroid.hpp:22: error: expected `,' or `...' before '&' token

You forgot "sf::" before of "Color".

Quote
Asteroid.hpp:23: error: expected class-name before '(' token

Here you must call the constructor of the base class, so sf::Shape. sf::Shape::Circle is a function that returns a sf::Shape, it's not a class or constructor, you can't call it like that.

You should also avoid to define your constructor twice ;)

Code: [Select]
Asteroid::Asteroid(float X, float Y, float Radius, const Color& Col, float Outline = 0.f, const Color& OutlineCol = sf::Color(0, 0, 0))
  : sf::Shape(Circle(X, Y, Radius, Col, Outline, OutlineCol)) // sf::Shape:: prefix is no longer necessary because you *are* in the scope of sf::Shape
{
}

Also, you should store your colors by value, don't just keep a reference to them.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
inheritance problem
« Reply #5 on: February 09, 2010, 06:09:23 pm »
Apart from that, you should consider aggregation. Don't inherit if you don't really need it. A
Code: [Select]
class Asteroid
{
    private:
        sf::Shape MyShape;
};
is much more flexible, even if inheritance seems to be "more practical" in the first term (i.e. less to type and to think). Just answer yourself if an Asteroid is a Shape and has to support all methods of sf::Shape. For example, I can't imagine Asteroid::AddPoint() to be a meaningful function. Keep dependencies small wherever you can – inheritance is a very strong dependency and shouldn't be employed recklessly.

And, identifiers that begin with underscore are reserved for the compiler and the StdLib implementation. Choose something like ASTEROID_HPP.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
inheritance problem
« Reply #6 on: February 10, 2010, 06:55:31 pm »
I tried to implent your ideas into my code but i think im losing it completly. Im not lazy.. Im on this problem now for hours and days. But im really screwed. I asked 3 different programmers, one of them with a C++ background, to solve the problem but unfortunalty they could not help me.

Could you be so kind and write the code for me?  Me and other people could use that code as an example for any future classes that inheritate from a SFML-class.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
inheritance problem
« Reply #7 on: February 10, 2010, 07:38:59 pm »
Forget about inheritance. You don't need it, really.
Code: [Select]
class Asteroid
{
public:

    Asteroid(/* whatever params you need */)
    {
        shape = sf::Shape::Circle(/* params */);
    }

private:

    sf::Shape shape;
};

Note: I didn't use the initializer list to make this code even more understandable ;)
Laurent Gomila - SFML developer

 

anything