这是我写的程序,但是一直出现 错误,请有心人帮帮忙
shape.h(4) : error C2011: “Shape”: “class”类型重定义
rectangle.h(4) : error C2504: “Shape”: 未定义基类
triangle.h(4) : error C2504: “Shape”: 未定义基类
这是我写的这个程序文件的下载地址,
http://pan.baidu.com/share/link?shareid=1845653991&uk=1059929726//Shape.h
#include<iostream>
using namespace std;
class Shape
{
public:
virtual void printArea(void);
};
//Cricle.h
#include"Shape.h"
using namespace std;
class Circle:public Shape
{
public:
Circle(float a):R(a){}
void printArea(void)
{cout<<3.1415926*R<<endl;}
private:
float R;
};
//Rectangle.h
#include"Shape.h"
using namespace std;
class Rectangle:public Shape
{
public:
Rectangle(float a,float b):Long(a),Wide(b){}
void printArea(void)
{cout<<Long*Wide<<endl;}
private:
float Long;
float Wide;
};
//Triangle.h
#include"Shape.h"
using namespace std;
class Triangle:public Shape
{
public:
Triangle(float a,float b):base(a),height(b){}
void printArea(void)
{cout<<0.5*base*height<<endl;}
private:
float base;
float height;
};
//main.cpp
#include"Circle.h"
#include"Rectangle.h"
#include"Triangle.h"
int main()
{
Circle CR(5.154);
CR.printArea();
Rectangle RE(6.124,5.1245);
RE.printArea();
Triangle TR(8.125,4.21);
TR.printArea();
return 0;
}
我在敲完之后,也发现了这个问题。去网上搜寻了一下,发现是Shpe.h多次编译造成的结果。
在Shape.h文件的答轿银前面添加#pragma once,即可解决清宴。这是我编写的Shape.h。
#pragma once
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void 帆老printArea() {}
};
可以参考:http://blog.csdn.net/m_leonwang/article/details/27678219
#include<iostream>晌扒
using namespace std;
class Shape
{
public:
virtual 宴坦昌void printArea(void); //用纯虚函数virtual void printArea(void)=0;信蠢
};