我打算编写一个使用虚函数进行多边形计算的程序,但是在完成程序后,会出现BLOCK_TYPE_IS_VALID(pHead - > nBlockUse)错误
// pointers to base class
#include <iostream>
#include "Polygon.h"
#include "Rectangle.h"
#include "Triangle.h"
using namespace std;
int main() {
Rectangle rect1(4, 5);
Rectangle rect2(3, 3);
Triangle tri(4, 4, false);
int triLength[3] = { 5, 4, 3 };
tri.setsideLength(triLength);
Polygon * p = &rect1;
cout << "Rectangle 1: " << endl;
cout << "\tArea: " << p->area() << endl;
cout << "\tSide: ";
p->printsideLength();
rect1.printsideLength();
cout << "\tTotal Side Length: " << p->totalsideLength() << endl;
p = &rect2;
cout << "Rectangle 2: " << endl;
cout << "\tArea: " << p->area() << endl;
cout << "\tSide: ";
p->printsideLength();
rect2.printsideLength();
cout << "\tTotal Side Length: " << p->totalsideLength() << endl;
p = &tri;
cout << "Triangle: " << endl;
cout << "\tArea: " << p->area() << endl;
cout << "\tSide: ";
p->printsideLength();
cout << "\tTotal Side Length: " << p->totalsideLength() << endl;
system("pause");
return 0;
}
这是程序的main.cpp,程序无需输入任何只需要显示结果的东西。
以下是三类(cpp&amp; h)
#ifndef Polygon_H
#define Polygon_H
#include <iostream>
using namespace std;
class Polygon{
private:
int noOfSide;
bool isAllSideEqual;
int* sideLength;
public:
Polygon();
Polygon(int n,bool s);
~Polygon();
void setsideLength(int* sl);
void printsideLength();
int totalsideLength();
virtual int area();
};
#endif
#include "Polygon.h"
#include <iostream>
using namespace std;
Polygon::Polygon(){
noOfSide = 3;
isAllSideEqual = false;
sideLength = new int[noOfSide];
sideLength = &sideLength[noOfSide];
};
Polygon::Polygon(int n,bool s){
if (n<3)
{
noOfSide = 3;
isAllSideEqual = false;}
else
{
noOfSide = n;
isAllSideEqual = s;
};
sideLength = new int[noOfSide];
sideLength = &sideLength[noOfSide];
};
Polygon::~Polygon(){
delete[] sideLength;
};
void Polygon::setsideLength(int* sl){
for(int i=0;i<noOfSide;i++)
sideLength[i] = sl[i];
};
void Polygon::printsideLength(){
for(int i=0;i<noOfSide;i++)
cout << sideLength[i] <<" ";
};
int Polygon::totalsideLength(){
int total = 0;
for(int i=0;i<noOfSide;i++)
total += sideLength[i];
return total;
};
int Polygon::area(){
return 0;
};
#ifndef Triangle_H
#define Triangle_H
#include <iostream>
#include "Polygon.h"
using namespace std;
class Triangle:public Polygon
{
private:
int width;
int height;
public:
Triangle(int w,int h,bool s);
virtual int area();
};
#endif
#include "Triangle.h"
#include <iostream>
using namespace std;
Triangle::Triangle(int w,int h,bool s){
width = w;
height = h;
Polygon(3,s);
};
int Triangle::area(){
int total = 0;
total = (width*height)/2;
return total;
};
#ifndef Rectangle_H
#define Rectangle_H
#include <iostream>
#include "Polygon.h"
using namespace std;
class Rectangle:public Polygon{
private:
int width;
int height;
public:
Rectangle(int w,int h);
void printsideLength();
virtual int area();
};
#endif
#include "Rectangle.h"
#include <iostream>
using namespace std;
Rectangle::Rectangle(int w,int h){
width=w;
height=h;
if(width = height)
Polygon(4,true);
else
Polygon(4,false);
int* size =new int[4];
size[0] = width;
size[1] = height;
size[2] = width;
size[3] = height;
Polygon::setsideLength(size);
};
void Rectangle::printsideLength(){
for(int i=0;i<2;i++)
cout<< width<<" "<<height <<" ";
};
int Rectangle::area(){
int total =0;
total = width*height;
return total;
};
程序没有任何编译错误,因此唯一的问题是关于内存 但哪里错了?虚函数部分是错误还是其他任何部分?
答案 0 :(得分:1)
有几个问题。
在Polygon
构造函数中,
sideLength = new int[noOfSide];
sideLength = &sideLength[noOfSide];
是主要的错误
它会使sideLength
指向您刚刚分配的内存的末尾
使用此指针是未定义的
您之后复制到该位置并将其传递给delete
,这两个都无效
您只需要sideLength = new int[noOfSide];
您还需要一个正确的复制构造函数和赋值运算符,因为该类是手动管理内存。
(你真正 应该做的是使用std::vector<int>
并停止担心内存分配。)
在您的子类构造函数中,Polygon(3,s);
等不会初始化您的基类,它们会创建一个未命名的Polygon
,并立即将其丢弃。
您应该初始化初始化列表中的基类(以及其他成员):
Triangle::Triangle(int w, int h, bool s)
: Polygon(3, s),
width(w),
height(h)
{
}
Rectangle
的构造函数存在同样的问题,并且使用了=
,您应该使用相等的==
添加问题。Rectangle::Rectangle(int w, int h)
: Polygon(4, w == h),
width(w),
height(h)
{
int size[] = {width, height, width, height};
setsideLength(size);
}
。
(如果启用警告,您的编译器可以向您发出警告。这样做,并听取它们的意见。)
public class MyClass<T extends MyInterface> {
private T myGeneric;
private String desc;
....
}
public interface MyInterface {
String getSomeString();
}
public class MySubClass implements MyInterface {
private String info;
....
@Override
public getSomeString() {
return "";
}
}