BLOCK_TYPE_IS_VALID(pHead -> nBlockUse) 错误
BLOCK_TYPE_IS_VALID(pHead -> nBlockUse) error
我要写一个程序,使用虚函数进行多边形计算,但是在我完成程序后,出现了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;
}
这是程序的主要.cpp,程序不需要输入任何东西只需要显示它的结果。
以下是三个类(cpp&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;
};
该程序没有任何编译错误,所以唯一的问题是关于内存但是哪里错了呢?是虚拟功能部分是错误的还是任何其他部分?
有几个问题。
在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);
}
相关文章:
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 调试断言失败 BLOCK_TYPE_IS_VALID (pHead->nblockuse) 从解构函数
- 使用shared_ptr<字符串>转换为一个无序集合<字符串>
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾