我在多态和纯虚函数方面存在问题。我的主类纯虚函数和继承
#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的类。 Line.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> \画\ drawview.cpp(53):看 '线' 的声明
我会用用'成员variable'命名命名功能*参数名*提醒你现在是'm_xxxxx'是去驱动任何习惯于处理以微软为中心的编码习惯的人** **疯狂**。现在杀死那只鸟**。 – WhozCraig 2013-03-27 16:43:17