它被称为抽象类,用于实现虚函数.它适用于四个类似实现中的三个

It is said abstract class for a virtual function that is implemented. It works with three of four similar implementations

本文关键字:实现 三个 四个 被称为 函数 适用于 抽象类 用于      更新时间:2023-10-16

这似乎只是circle.cpp的问题,即使代码在point.cpp中看起来相同。我一直在看代码很长一段时间了,我没有发现这两个文件有什么不同。下面是错误信息:

>------ Build started: Project: ou5, Configuration: Release Win32 ------
1>  circle.cpp
1>  ou5.cpp
1>c:usersjonasdocumentsvisual studio 2010projectsou5ou5shapeptr.h(52): error C2259: 'Shape' : cannot instantiate abstract class
1>          due to following members:
1>          'Shape *Shape::clone(void) const' : is abstract
1>          c:usersjonasdocumentsvisual studio 2010projectsou5ou5shape.h(21) : see declaration of 'Shape::clone'
1>ou5.cpp(20): error C2259: 'Shape' : cannot instantiate abstract class
1>          due to following members:
1>          'Shape *Shape::clone(void) const' : is abstract
1>          c:usersjonasdocumentsvisual studio 2010projectsou5ou5shape.h(21) : see declaration of 'Shape::clone'
1>ou5.cpp(27): error C2259: 'Shape' : cannot instantiate abstract class
1>          due to following members:
1>          'Shape *Shape::clone(void) const' : is abstract
1>          c:usersjonasdocumentsvisual studio 2010projectsou5ou5shape.h(21) : see declaration of 'Shape::clone'
1>  point.cpp
1>  polygon.cpp
1>  rectangle.cpp
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

带有纯虚函数的基类代码:

#ifndef SHAPE_H
#define SHAPE_H
using namespace std;
#include <iostream>
#include <ostream>
class Shape
{
public:
    Shape()
    {
        cout << "Shape constructor" << endl;
    }
    Shape(double xval, double yval)
    {
        x = xval;
        y = yval;
    }

    virtual Shape* clone() const = 0;
    virtual double area()
    {
        return 1;
    }
    virtual void print() const{}
    virtual ostream& printf(ostream &os){return os;}
    /* // Copy constructor
    Shape(const Shape *sp)
    {
        // Copy the string into our newly allocated memory
        sp =this->clone();
    }*/
    /*Shape & operator=(const Shape & sptr)
        {
    if(this!= &sptr)
    {
        *this = sptr.clone();
    }
    return *this;
}*/
    double getx() const
    {
        return x;
    }
    double gety() const
    {
        return y;
    }
    virtual ~Shape() {cout << "Shape raderas av destruktor" <<endl; }
        protected:
    double x;       //Attribut
    double y;       //Attribut
    Shape *sp;
};
#endif

正在工作的点h:

#ifndef POINT_H
#define POINT_H
#include "shape.h"
using namespace std;
#include <iostream>
#include <fstream>
class Point : public Shape
{
public:
    Point()
    {
        cout << "Point constructor" << endl;
    }
    Point(double x, double y, double sz) : Shape(x,y), size(sz)
    {
        cout << "Point constructor" << endl;
    }
    void las(istream &is)
    {
        is >> x >> y >> size;
    }
    ostream& printf(ostream &os) ;
    void print() const;
    double area();
    Shape* clone() const;
    virtual ~Point(){cout << "Point raderas av destruktor" << endl;}
private:
    double size;        //Attribut
};
#endif

point.cpp:

#include "point.h"
#include <ostream>
using namespace std;
Shape* Point::clone() const { return new Point( *this); }
void Point::print() const
    {
    cout << "POINT: ";
    cout <<"(" << this->x<<"," << this->y << ")" << this->size << endl;
    }
ostream& Point::printf(ostream &os) 
    {
    os << "POINT: ";
    os <<"(" << this->x<<"," << this->y << ")" << this->size << endl;
    return os;
    }
    double Point::area()
    {
        return size;
    }

有问题的文件。第一个circle.h:

#ifndef CIRCLE_H
#define CIRCLE_H
#include "shape.h"
using namespace std;
#define M_PI 3.141592654
#include <fstream>
#include <iostream>
class Circle : public Shape
{
public:
    Circle()
    {
        cout << "Circle constructor" << endl;
    }
    Circle(double x, double y, double rad) : Shape(x,y), radie(rad)
    {
        cout << "Circle constructor" << endl;
    }
    void las(istream &is)
    {
        is >> x >> y >> radie;
    }
    ostream& printf(ostream &os);
    double area(); 
    void print() const;
    Shape* clone() const;
    virtual ~Circle(){cout << "Circle raderas av destruktor" << endl;}
private:
    double radie;       //Attribut
};
#endif

然后circle.cpp:

#include "circle.h"
using namespace std;
#include <ostream>
Shape* Circle::clone() const {return new Circle( *this);}
double Circle::area()
    {
        double tmparea;
        tmparea = M_PI * radie * radie;
        return tmparea;
    }
    void Circle::print() const
    {
    cout << "CIRCLE: ";
    cout <<"(" << this->x<<"," << this->y << ")" << this->radie << endl;
    }
    ostream& Circle::printf(ostream &os) 
    {
    os << "CIRCLE: ";
    os <<"(" << this->x<<"," << this->y << ")" << this->radie << endl;
    return os;
    }

问题是clone,它在文件工作和不工作的文件中看起来是一样的。工作文件是point.cpp,但它也可以与其他两个文件一起工作。

我在这里添加了shapeptr.h:

#ifndef SHAPEPTR_H
#define SHAPEPTR_H
#include "shape.h"
#include "circle.h"
#include "point.h"
#include "polygon.h"
#include "rectangle.h"
#include <ostream>
#include <istream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

class ShapePtr
{
 public:
     ShapePtr()
     {
         sptr = 0;
         numshapes = numshapes + 1;
     }
  ShapePtr(Shape * v)//: Shape(v->getx(),v->gety())
  {
      sptr = v;
      numshapes = numshapes + 1;
  }
  // Copy constructor
ShapePtr(const Shape &sp)
{
        // Copy the string into our newly allocated memory
        sptr = sp.clone();
}
const ShapePtr & ShapePtr::operator=(const Shape & sp)
{
    if(this->sptr!= &sp)
    {
        sptr = sp.clone();
    }
    return *this;
}
  ~ShapePtr(){
      numshapes = numshapes - 1;  
      cout << "ShapePtr raderas av destruktor" << endl;
  }  
 Shape getshape()
 {
     return *sptr;
 }
 private:
  Shape *sptr;
public:
  static int numshapes;
  friend istream& operator>>(istream &is, ShapePtr & sp);
  friend ostream& operator<<(ostream &os, const ShapePtr & sp);
};
int ShapePtr::numshapes = 0;
ostream& operator<<(ostream &os, const ShapePtr & sp)
{
    os << sp.sptr->printf(os);
    return os;
}
istream& operator>>(istream &is, ShapePtr & sp)
{
    string check;
    while(!is.eof())
    {
        is >> check;
        if(check == "Circle")
        {
        Circle cir;
        cir.las( is );
        sp = ShapePtr(new Circle(cir));
        return is;
        }
        if(check == "Polygon")
        {
        Polygon pol;
        pol.las( is );
        sp = ShapePtr(new Polygon(pol));
        return is;
        }
        if(check == "Point")
        {
        Point poi;
        poi.las( is );
        sp = ShapePtr(new Point(poi));
        return is;
        }
        if(check == "Rectangle")
        {
        Rectangle rec;
        rec.las( is );
        sp = ShapePtr(new Rectangle(rec));
        return is;
        }
    }
    }

#endif

根据您的评论,您正试图在Shapeptr.h中创建一个无效的abstract class实例。您尝试创建这个实例的位置在这里:

Shape getshape()
{
     return *sptr;
}

正如Mark b指出的那样,修复方法是将返回类型更改为Shape&

相关文章: