MFC上Rectangle类的诡异问题,无法初始化对象,而换一个类,比如Rect,代码相同却可以初始化!
Shape类同时还有一个Circle类,Circle没问题#ifndefShape_h#defineShape_hclassShape{public:virtualdou...
Shape类 同时还有一个Circle类,Circle没问题
#ifndef Shape_h
#define Shape_h
class Shape
{
public:
virtual double ZhouChang()=0;
virtual double Area()=0;
};
#endif
Rectangle.h
#include "Shape.h"
class Rectangle:public Shape
{
private:
int x,y;
public:
Rectangle(int,int);
double ZhouChang();
double Area();
};
Rectangle.cpp
#include "StdAfx.h"
#include "Rectangle.h"
Rectangle::Rectangle(int x,int y)
{
this->x=x;
this->y=y;
}
double Rectangle::ZhouChang()
{
return (x+y)*2;
}
double Rectangle::Area()
{
return x*y;
}
OnDraw 函数 已添加头文件 Rectangle.h
void CEx3_ClassShapeView::OnDraw(CDC* pDC)
{
CEx3_ClassShapeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
double TotalArea=0;
Circle c1(10.0);
Circle c2(20.0);
Rectangle r1(3,3);
Shape *pS[]={&c1,&c2,&r1};
for (int i=0;i<3;i++)
{
TotalArea+=pS[i]->Area();
}
CString str;
str.Format("总面积为:%.2f",TotalArea);
pDC->TextOut(100,100,str);
} 展开
#ifndef Shape_h
#define Shape_h
class Shape
{
public:
virtual double ZhouChang()=0;
virtual double Area()=0;
};
#endif
Rectangle.h
#include "Shape.h"
class Rectangle:public Shape
{
private:
int x,y;
public:
Rectangle(int,int);
double ZhouChang();
double Area();
};
Rectangle.cpp
#include "StdAfx.h"
#include "Rectangle.h"
Rectangle::Rectangle(int x,int y)
{
this->x=x;
this->y=y;
}
double Rectangle::ZhouChang()
{
return (x+y)*2;
}
double Rectangle::Area()
{
return x*y;
}
OnDraw 函数 已添加头文件 Rectangle.h
void CEx3_ClassShapeView::OnDraw(CDC* pDC)
{
CEx3_ClassShapeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
double TotalArea=0;
Circle c1(10.0);
Circle c2(20.0);
Rectangle r1(3,3);
Shape *pS[]={&c1,&c2,&r1};
for (int i=0;i<3;i++)
{
TotalArea+=pS[i]->Area();
}
CString str;
str.Format("总面积为:%.2f",TotalArea);
pDC->TextOut(100,100,str);
} 展开
3个回答
展开全部
加一个命名空间或者换个名字吧,命名冲突了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个是Win32API
The Rectangle function draws a rectangle. The rectangle is outlined by using the current pen and filled by using the current brush.
BOOL Rectangle(
HDC hdc, // handle to DC
int nLeftRect, // x-coord of upper-left corner of rectangle
int nTopRect, // y-coord of upper-left corner of rectangle
int nRightRect, // x-coord of lower-right corner of rectangle
int nBottomRect // y-coord of lower-right corner of rectangle
);
Requirements
Windows NT/2000/XP/Vista: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Wingdi.h; include Windows.h.
Library: Use Gdi32.lib.
The Rectangle function draws a rectangle. The rectangle is outlined by using the current pen and filled by using the current brush.
BOOL Rectangle(
HDC hdc, // handle to DC
int nLeftRect, // x-coord of upper-left corner of rectangle
int nTopRect, // y-coord of upper-left corner of rectangle
int nRightRect, // x-coord of lower-right corner of rectangle
int nBottomRect // y-coord of lower-right corner of rectangle
);
Requirements
Windows NT/2000/XP/Vista: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Wingdi.h; include Windows.h.
Library: Use Gdi32.lib.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Rectangle 是WIN32 API中的一个 结构体。你最好改一下名字。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询