BLOCK_TYPE_IS_VALID(pHead -> nBlockUse) 错误

BLOCK_TYPE_IS_VALID(pHead -> nBlockUse) error

本文关键字:gt nBlockUse 错误 pHead IS TYPE VALID BLOCK      更新时间:2023-10-16

我要写一个程序,使用虚函数进行多边形计算,但是在我完成程序后,出现了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);
}