它被称为抽象类,用于实现虚函数.它适用于四个类似实现中的三个
It is said abstract class for a virtual function that is implemented. It works with three of four similar implementations
这似乎只是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&
。
相关文章:
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 我有三个 getline,但是一旦编译,输入就太多了
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 有没有更好的方法对C++中的三个整数进行排序?
- 如何检查第三个 API 是否在 Linux 中为 c/c++ 程序创建了一个新线程?
- 为什么 getch 在按下函数或箭头键时返回三个值?
- 使用三个数字比较器进行排序
- 我想在C++代码中比较这三个术语
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 我该如何编码,使计算机知道两个名称条目和三个名称条目之间的区别
- 为什么我需要三个嵌套的大括号来调用赋值运算符,将const引用到二维数组
- 如何通过通用引用或std::forward将这三个c++模板函数合并为一个
- 首先处理第二个和第三个堆与第一个和第二个堆之间的逻辑差异
- count_if使用我自己的类实现第三个参数
- 用c++实现程序,通过附加另外两个向量来输出第三个向量
- 如何将主文件分成三个文件:头文件、客户端和实现
- 它被称为抽象类,用于实现虚函数.它适用于四个类似实现中的三个