我遇到了多态性和纯虚函数的问题。我的主要课程
#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);
};
然后我有这个派生自Shape的类。线.h
#pragma once
#include<memory>
class Line: public Shape
{
public:
void Draw(Gdiplus::Graphics & m_GraphicsImage);
}
这是我的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);
}
由于多态性的原因,在我的主要声明中,我声明了一个 Shape 类型的共享指针
std::shared_ptr<Shape> m_shape;
然后尝试调用 Line.cpp 的函数,但它不起作用,
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;
现在我得到 drawview.cpp(54): error C2371: 'line' : redefinition; 不同的基本类型 1> \draw\drawview.cpp(53) : 见'line'的声明