Reputation: 191
I'm having problems with polymorphism and pure virtual functions. My main class
#include<memory>
class Shape
{
public:
Gdiplus::Point start;
Gdiplus::Point end;
std::shared_ptr<Gdiplus::Pen> m_pen;
virtual void Draw(Gdiplus::Graphics & m_GraphicsImage) = 0;
void setPen(std::shared_ptr<Gdiplus::Pen> pen2);
void setStart(int xPos, int yPos);
void setEnd(int xCor, int yCor);
};
Then i have this class that derives from Shape. Line.h
#pragma once
#include<memory>
class Line: public Shape
{
public:
void Draw(Gdiplus::Graphics & m_GraphicsImage);
}
This is my line.cpp.
#include "stdafx.h"
#include "Line.h"
#include "ShapeMaker.h"
void Line::Draw(Gdiplus::Graphics & m_GraphicsImage)
{
m_GraphicsImage.DrawLine(m_pen.get(),start.X,start.Y,end.X,end.Y);
}
On my main i declare a shared pointer of type Shape for polymorphism reasons
std::shared_ptr<Shape> m_shape;
and then try and call functions for the Line.cpp but it doesn't work,
LRESULT CDrawView::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
int xPos= GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
start.X = xPos;
start.Y = yPos;
//Line line;
auto line = std::make_shared<Shape> (m_shape);
std::shared_ptr<Gdiplus::Pen> myPen(pen.Clone());
line->setPen(myPen);
line->setStart(xPos,yPos);
return 0;
}
LRESULT CDrawView::OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
int xPos= GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
end.X = xPos;
end.Y = yPos;
//Pen pen(Color(0, 0, 255));
//Line line;
auto line = std::make_shared<Shape> (m_shape);
line->setEnd(xPos,yPos);
line->Draw(m_GraphicsImage);
m_shape.reset();
RedrawWindow();
return 0;
} Now I'm getting drawview.cpp(54): error C2371: 'line' : redefinition; different basic types 1> \draw\drawview.cpp(53) : see declaration of 'line'
Upvotes: 0
Views: 273
Reputation: 83
The error is for instantiating Shape, which is abstract class.
The above syntax 'auto line = std::make_shared (m_shape);' is the culprit.
In this statement, you are trying to instantiate Shape class.
Refer to this http://www.cplusplus.com/reference/memory/dynamic_pointer_cast/
for correct usage of casting between shared_ptr
Upvotes: 0
Reputation: 2802
I believe the problem is your constructor
std::make_shared<Shape> (m_shape);
Which is essentially new Shape (an abstract class). Instead you should use
std::make_shared<Line> (m_shape);
Upvotes: 2
Reputation: 500923
The line
void Draw(Gdiplus::Graphics & m_GraphicsImage)
should read
void Line::Draw(Gdiplus::Graphics & m_GraphicsImage)
Without the Line::
you are defining a free function called Draw()
that happens to have the same signature as Line::Draw()
but is otherwise unrelated to it.
Upvotes: 0