初学C++ 对类的头文件和.cpp文件分开写不熟悉
用c++和easyX做一个画板
写类的头文件和.cpp
主函数测试画圆时爆出以下错误
shape是图形超类 shape.h如下
#if !defined(AFX_SHAPE_H__ED70CC32_2F86_41DF_B340_5D695BBF9F73__INCLUDED_)
#define AFX_SHAPE_H__ED70CC32_2F86_41DF_B340_5D695BBF9F73__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class shape
{
public:
short x[4];
virtual int draw();
short * getMouse();
};
#endif // !defined(AFX_SHAPE_H__ED70CC32_2F86_41DF_B340_5D695BBF9F73__INCLUDED_)
shape.cpp
// shape.cpp: implementation of the shape class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "shape.h"
#include <iostream>
#include <math.h>
#include <stdio.h>
#include "graphics.h"
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
short x[4];
int draw()
{
cout << "draw a shape" << endl;
return 1 ;
}
short * getMouse(){
ExMessage m;
int flag=0 ;
double d=0;
while(flag==0){
m= getmessage(EM_MOUSE | EM_KEY);//获取两次鼠标事件
switch(m.message){
case WM_LBUTTONDOWN:
x[0]=m.x;
x[1]=m.y;
putpixel(m.x, m.y, YELLOW);
break;
case WM_LBUTTONUP:
x[2]=m.x;
x[3]=m.y;
flag=1;
break;
case WM_RBUTTONDOWN:///右键结束当前图形
return 0 ;
}
}
return x; //返回两个坐标
}
circle类继承shape
circle.h如下
// circle.h: interface for the circle class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CIRCLE_H__1A3366EE_6CA9_4C72_A4D8_7B71A5C0EDCC__INCLUDED_)
#define AFX_CIRCLE_H__1A3366EE_6CA9_4C72_A4D8_7B71A5C0EDCC__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "shape.h"
class circle : public shape
{
public:
circle();
virtual int draw();
};
#endif // !defined(AFX_CIRCLE_H__1A3366EE_6CA9_4C72_A4D8_7B71A5C0EDCC__INCLUDED_)
circle.cpp:
// circle.cpp: implementation of the circle class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "circle.h"
#include <iostream>
#include <math.h>
#include <stdio.h>
#include "graphics.h"
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
circle::circle(){
}
int draw(){
cout << "draw a Circle" << endl;
double r;
short* x;
x=shape().getMouse();
if(x==0)
{
return 0;
}
r=sqrt((x[2]-x[0])*(x[2]-x[0])+(x[3]-x[1])*(x[3]-x[1]));
//计算半径
printf("x0=%d,x1=%d\n",x[0],x[1]);
printf("x0=%d,x1=%d\n",x[2],x[3]);
printf("r=%lf\n",r);
circle(x[0],x[1],r);
return 1 ;
}
主函数运行到new circle 时报错