新手求助:为什么同一个程序中的虚函数,有一个不能运行?
近日,在 Visual Studio .Net 2008 的 VC++ 上运行 Deitel 的《C++程序设计教程》中的一个例程( 图10.12-图10.20),却发生了错误,错误及程序如下,请各位指教,多谢!
==========================================================================================================================
错误 1 error C2084: 函数“double Cylinder::getArea(void) const”已有主体 d:\backup\我的文档\visual studio 2008\projects\c++deitel10-20\c++deitel10-20\cylinder.cpp 23 C++deitel10-20
===========================================================================================================================
Shape.h
-------------------------------------------------
#ifndef SHAPE_H
#define SHAPE_H
#include <string>
using namespace std;
class Shape
{
public:
virtual double getArea() const;
virtual double getVolume() const;
virtual string getName() const = 0;
virtual void print() const = 0;
};
#endif
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Shape.cpp
----------------------------------------------------
#include <iostream>
using namespace std;
#include "shape.h"
double Shape::getArea() const
{
return 0.0;
}
double Shape::getVolume() const
{
return 0.0;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Point.h
--------------------------------------------------
#ifndef POINT_H
#define POINT_H
#include "shape.h"
class Point : public Shape
{
public:
Point( int = 0, int = 0 );
void setX( int );
int getX() const;
void setY( int );
int getY() const;
virtual string getName() const;
virtual void print() const;
private:
int x;
int y;
};
#endif
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Point.cpp
-----------------------------------------------------
#include <iostream>
using namespace std;
#include "point.h"
Point::Point( int xValue, int yValue ) : x( xValue ), y( yValue )
{
}
void Point::setX( int xValue )
{
x = xValue;
}
int Point::getX() const
{
return x;
}
void Point::setY( int yValue )
{
y = yValue;
}
int Point::getY() const
{
return y;
}
string Point::getName() const
{
return "Point";
}
void Point::print() const
{
cout << "[ " << getX() << ", " << getY() << " ]";
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Circle.h
---------------------------------------------------
#ifndef CIRCLE_H
#define CIRCLE_H
#include "point.h"
class Circle : public Point
{
public:
Circle( int = 0, int = 0, double = 0.0 );
void setRadius( double );
double getRadius() const;
double getDiameter() const;
double getCircumference() const;
virtual double getArea() const;
virtual string getName() const;
virtual void print() const;
private:
double radius;
};
#endif
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Circle.cpp
-------------------------------------------------------
#include <iostream>
using namespace std;
#include "circle.h"
Circle::Circle( int xValue, int yValue, double radiusValue ) : Point( xValue, yValue )
{
setRadius( radiusValue );
}