我对每次执行复制构造函数和
operator=
时反复出现的“调试断言失败!_CrtIsValidHeapPointer(block)”错误感到困惑。尽管尝试了 std::vector 方法并付出了相当大的努力,我探索了与内存管理、验证指针等相关的各种建议,但这些方法似乎都没有解决这个问题。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
class Aliment {
private:
char* nume;
float pret;
int nr_luni;
vector<float> istoric_preturi;
public:
Aliment() {
this->nume = new char[strlen("DEFAULT")+1];
memcpy(this->nume, "DEFAULT", strlen("DEFAULT")+1);
this->pret = 0.0f;
this->nr_luni = 0;
}
Aliment(const char* nume) {
this->nume = new char[strlen(nume)+1];
memcpy(this->nume, nume, strlen(nume)+1);
this->pret = 5.67f;
this->nr_luni = 3;
this->istoric_preturi.push_back(5.69f);
this->istoric_preturi.push_back(6.0f);
this->istoric_preturi.push_back(5.87f);
}
~Aliment() {
//delete[] this->istoric_preturi;
delete[] this->nume;
cout << endl << "S-a apelat!";
}
void afisareAliment() {
cout << endl << "Alimentul " << this->nume << " are pretul actual de " << this->pret << " lei.";
cout << endl << "Istoricul preturilor in ultimele " << this->nr_luni << " luni este: ";
if (this->nr_luni != 0) {
for (int i = 0; i < this->nr_luni; i++)
cout << this->istoric_preturi[i] << " ";
}
}
};
enum Tip{LAPTE,BRANZA,CASCAVAL,SMANTANA};
class ProdusLactat : public Aliment {
private:
const int id;
char* brand;
int nrZile;
int* bucatiVandute=nullptr;
Tip tip;
bool expiraInCurand;
float volum;
int nrBucatiStoc;
public:
ProdusLactat():Aliment(),id(0) {
this->brand = new char[strlen("DEFAULT") + 1];
memcpy(this->brand, "DEFAULT", strlen("DEFAULT")+1);
this->nrZile = 0;
this->tip = LAPTE;
this->expiraInCurand = 0;
this->volum = 0.0f;
this->nrBucatiStoc = 0;
}
ProdusLactat(const char* nume, int id, const char* brand, Tip tip) :Aliment(nume), id(id) {
this->brand = new char[strlen(brand) + 1];
memcpy(this->brand, brand, strlen(brand) + 1);
this->nrZile = 3;
this->bucatiVandute = new int[this->nrZile] {6, 6, 7};
this->tip = tip;
this->expiraInCurand = 1;
this->volum = 1.5f;
this->nrBucatiStoc = 10;
}
ProdusLactat(const char* nume, int id, const char* brand,int zile,int* vandute,Tip tip, bool expira, float volum, int stoc) :Aliment(nume),id(id) {
this->brand = new char[strlen(brand) + 1];
memcpy(this->brand, brand, strlen(brand) + 1);
this->nrZile = zile;
this->bucatiVandute = new int[this->nrZile];
for (int i = 0; i < this->nrZile; i++)
this->bucatiVandute[i] = vandute[i];
this->tip = tip;
this->expiraInCurand = expira;
this->volum = volum;
this->nrBucatiStoc = stoc;
}
~ProdusLactat() {
delete[] this->brand;
}
ProdusLactat(const ProdusLactat& pl): Aliment(pl),id(pl.id){
if (pl.brand) {
this->brand = new char[strlen(pl.brand) + 1];
memcpy(this->brand, pl.brand, strlen(pl.brand) + 1);
}
else this->brand = nullptr;
this->nrZile = pl.nrZile;
this->bucatiVandute = new int[this->nrZile];
if (pl.bucatiVandute && pl.nrZile) {
for (int i = 0; i < this->nrZile; i++)
this->bucatiVandute[i] = pl.bucatiVandute[i];
}
else this->bucatiVandute = nullptr;
this->tip = pl.tip;
this->expiraInCurand = pl.expiraInCurand;
this->volum = pl.volum;
this->nrBucatiStoc = pl.nrBucatiStoc;
}
ProdusLactat& operator=(const ProdusLactat& produs) {
if (this != &produs) {
Aliment::operator=(produs);
delete[] this->brand;
delete[] this->bucatiVandute;
if (produs.brand) {
this->brand = new char[strlen(produs.brand) + 1];
memcpy(this->brand, produs.brand, strlen(produs.brand) + 1);
}
else this->brand = nullptr;
this->nrZile = produs.nrZile;
this->bucatiVandute = new int[this->nrZile];
if (produs.bucatiVandute && produs.nrZile) {
for (int i = 0; i < this->nrZile; i++)
this->bucatiVandute[i] = produs.bucatiVandute[i];
}
else this->bucatiVandute = nullptr;
this->tip = produs.tip;
this->expiraInCurand = produs.expiraInCurand;
this->volum = produs.volum;
this->nrBucatiStoc = produs.nrBucatiStoc;
}
return *this;
}
int operator[](int poz) {
if (poz >= 0 && poz < this->nrZile)
return this->bucatiVandute[poz];
else throw out_of_range("Pozitie in afara intervalului!");
}
bool operator>(const ProdusLactat& pl) {
int suma1 = 0, suma2 = 0;
for (int i = 0; i < this->nrZile; i++)
suma1 += this->bucatiVandute[i];
for (int i = 0; i < pl.nrZile; i++)
suma2 += pl.bucatiVandute[i];
return suma1 > suma2;
}
char* getBrand() {
char* brandDeReturnat = new char[strlen(this->brand) + 1];
memcpy(brandDeReturnat, this->brand, strlen(this->brand) + 1);
return brandDeReturnat;
}
void setBrand(const char* brandNou) {
if (brandNou != nullptr) {
if (this->brand != nullptr)
delete[] this->brand;
this->brand = new char[strlen(brandNou) + 1];
memcpy(this->brand, brandNou, strlen(brandNou) + 1);
}
}
void afisareProdusLactat() {
cout << "\nProdusul lactat are id ul: " << this->id;
//Aliment::afisareAliment();
this->afisareAliment();
cout << "\nBrand: " << this->brand;
cout << "\nIstoricul de vanzari in ultimele " << this->nrZile << " zile este: ";
if (this->nrZile) {
for (int i = 0; i < this->nrZile; i++)
cout << this->bucatiVandute[i] << " ";
}
cout << "\nTipul produsului: ";
switch (this->tip) {
case 0:
cout << "LAPTE";
break;
case 1:
cout << "BRANZA";
break;
case 2:
cout << "CASCAVAL";
break;
case 3:
cout << "SMANTANA";
break;
}
cout << "\nExpira in curand? ";
if (this->expiraInCurand == 0)
cout << "NU";
else cout << "DA";
cout << "\nAre volumul: " << this->volum;
cout << "\nExista " << this->nrBucatiStoc << " bucati ramase pe stoc. ";
cout << "\n";
}
};
int main() {
ProdusLactat produs;
produs.afisareProdusLactat();
ProdusLactat produs2("Branza cremoasa", 1, "Hochland", BRANZA);
produs2.afisareProdusLactat();
ProdusLactat produs3("Smantana lichida", 2, "Boni", 3, new int[3] {8, 7, 20}, SMANTANA, 0, 0.7f, 345);
produs3.afisareProdusLactat();
cout << "\nIn ziua 1 s-au vandut "<<produs3[0]<<" bucati din produs3";
if (produs2 > produs3)
cout << "\nProdusul 2 se vinde mai bine decat produsul 3.";
else if (produs3 > produs2)
cout << "\nProdusul 3 se vinde mai bine decat produsul 2.";
else cout << "\nAmbele produse se vand la fel de bine.";
cout << "\nBrand pt produs 3: " << produs3.getBrand();
produs3.setBrand("Pilos");
cout << "\nBrand pt produs 3: " << produs3.getBrand()<<"\n";
/*ProdusLactat produs4(produs3);
produs4.afisareProdusLactat();*/
ProdusLactat produs5;
produs5 = produs3;
produs5.afisareProdusLactat();
}
您好,我也想知道答案,因为我面临着类似的问题,亲爱的安德鲁·斯科特。