gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:28:57 24 4
gpt4 key购买 nike

我打算写一个程序,使用虚函数做多边形计算,但是当我完成这个程序后,出现 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com