c++ - BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)错误

标签 c++

我打算写一个程序,使用虚函数做多边形计算,但是当我完成这个程序后,出现 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,程序不需要输入任何东西只需要显示它的结果。

这里是三个class(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);
}

关于c++ - BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36598722/

相关文章:

c++ - 如何在 C++ 中编写一个通用函数,将整数和字符串作为参数并修改它们?

c++ - 通知 GUI 对象有关屏幕大小的信息 - 设计

c++ - 如何在 Linux 上的 C++ 程序中使用 yaml-cpp?

c++ - 声明变量时发生访问冲突

java - native 方法上的 JNI UnsatisfiedLinkError

c++ - 引用内存中的数组

c++ - 在 Windows 窗体中使用 OpenCV

C++ 声明一个基于非常量变量的数组?

c++ - 为什么我不能将 std::from_chars 的结果分配给 std::tie?

c++ - 使用 std::vector<std::string> myString 时出错