I'm struggling to use inheritance. I am studying computer science and have to work it out... Rihgt now I 'm trying to make my own class 'Copy' which contains a reference to a 'Drawable' object, For instance I have an object of the class 'circle' which inherits from 'Drawable'. I want to make a copy of the said object 'circle', which seems to work out ok. But when I want to call upon the 'draw' method things go sour.
Error Message:
1>------ Build started: Project: Project2, Configuration: Debug Win32 ------
1> main.cpp
1> Drawable.cpp
1> copy.cpp
1> Cirkel.cpp
1> Generating Code...
1>Cirkel.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Drawable::draw(class sf::RenderWindow &,class sf::Vector2<float>)" (?draw@Drawable@@UAEXAAVRenderWindow@sf@@V?$Vector2@M@3@@Z)
1>copy.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Drawable::draw(class sf::RenderWindow &,class sf::Vector2<float>)" (?draw@Drawable@@UAEXAAVRenderWindow@sf@@V?$Vector2@M@3@@Z)
**This is my main: **
#include <SFML\Graphics>
#include "circle.hpp"
#include "copy.hpp"
int main(){
sf::RenderWindow window{ sf::VideoMode{ 640, 480 }, "SFML window" };
circle blauwe_circle{ 40.0F, sf::Color::Blue };
circle rode_circle{ 10.F, sf::Color::Red };
copy cBlauw{ sf::Vector2f{ 40.f, 40.f }, blauwe_circle };
copy cRoodA{ sf::Vector2f{ 50.f, 50.f }, rode_circle };
sf::Vector2f nul{ 0.f, 0.f };
while (window.isOpen()){
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed) window.close();
}
sf::sleep(sf::milliseconds(20));
}
return 0;
}`
drawable.hpp
class Drawable
{
public:
virtual void draw(sf::RenderWindow & window, sf::Vector2f vector);
};
Circle.hpp:
#include "Drawable.hpp"
class circle : public Drawable{
public:
circle(float size = 30, sf::Color color = sf::Color::Red);
void draw(sf::RenderWindow & window, sf::Vector2f position) override;
private:
float size;
sf::Color color;
sf::CircleShape c;
};
cirlce.cpp
#include "circle.hpp"
#include "Drawable.hpp"
circle::circle(float fSize, sf::Color color) :
size{ fSize },
color{ color }
{
c.setRadius(size);
c.setPointCount(30U);
c.setFillColor(color);
}
void circle::draw(sf::RenderWindow & window, sf::Vector2f position){
c.setPosition(position);
window.draw(c);
}
copy.hpp:
#include "Drawable.hpp"
class copy: public Drawable
{
public:
copy(sf::Vector2f position, Drawable & c);
void draw(sf::RenderWindow & window, sf::Vector2f vector) override;
private:
sf::Vector2f position;
Drawable & c;
};
copy.cpp
#include "copy.hpp"
#include "Drawable.hpp"
copy::copy(sf::Vector2f vPosition, Drawable & c) :
position{ vPosition },
c{ c }
{}
void copy::draw(sf::RenderWindow & window, sf::Vector2f v){
c.draw(window, v);
}
If Drawable::draw
is intended to be a pure virtual function without implementation, add = 0
:
class Drawable
{
public:
virtual void draw(sf::RenderWindow & window, sf::Vector2f vector) = 0;
};