删除[]搞砸了我的程序,发生了什么

时间:2016-03-30 01:46:27

标签: c++

我以各种方式查看了我的程序而且我已经尝试了所有内容但我无法弄清楚为什么delete[]会崩溃我的程序。

头文件

#ifndef SICT_PRODUCT_H
#define SICT_PRODUCT_H
#include "Streamable.h"
#include "general.h"

namespace sict{

class Product: public Streamable{
    char sku_[MAX_SKU_LEN+1];
    char* name_;
    double price_;
    bool taxed_;
    int quantity_;
    int qtyNeeded_;

public:
    Product();
    Product(const char* sku,const char* name, bool tax=true, double price=0,int needed=0);
    Product& operator=(Product &P);
    ~Product();
    void sku(char* sku);
    void price(double price);
    void name(char* name);
    void taxed(bool tax);
    void quantity(int q);
    void qtyNeeded(int n);
    const char* sku()const;
    double price()const;
    const char* name()const;
    bool taxed()const;
    int quantity()const;
    int qtyNeeded()const;
    bool isEmpty()const;
    bool operator==(const char* name);
    int operator+=(int num);
    int operator-=(int num);
    double cost()const;
};

double operator+=(double& num,const Product& P);
std::ostream& operator<<(std::ostream& os,const Product& P);
std::istream& operator>>(std::istream& is, Product& P);

}

#endif

.cpp文件

#include <iostream>
#include <cstring>
#include "Product.h"
using namespace std;

namespace sict{

Product::Product(){
    sku_[0]=0;
    name_=nullptr;
    price_=0;
    taxed_=0;
    quantity_=0;
    qtyNeeded_=0;
}

Product::Product(const char* sku,const char* name, bool tax, double price, int needed){
    strncpy(sku_, sku, strlen(sku) + 1);
    sku_[strlen(sku) + 1] = 0;
    if (name != nullptr || name != 0){
        delete[] name_;
        name_ = new char[strlen(name) + 1];
        strcpy(name_, name);
    }
    quantity_=0;
    taxed_=tax;
    price_=price;
    qtyNeeded_=needed;
}

Product& Product::operator=(Product &P){
    strncpy(sku_, P.sku_, strlen(P.sku_) + 1);
    sku_[strlen(P.sku_) + 1] = 0;
    delete[] name_;
    name_ = new char[strlen(P.name_) + 1];
    strcpy(name_, P.name_);
    taxed_ = P.taxed_;
    price_ = P.price_;
    qtyNeeded_ = P.qtyNeeded_;
    quantity_=P.quantity_;
    return *this;
}

Product::~Product(){
    delete[] name_;
    name_=nullptr;
}

void Product::sku(char* sku){
    strncpy(sku_,sku,strlen(sku)+1);
    sku_[strlen(sku)+1]=0;
}

void Product::price(double price){
    price_=price;
}

void Product::name(char* name){
    delete[] name_;
    name_= new char[strlen(name)+1];
    strcpy(name_,name);
}

void Product::taxed(bool tax){
    taxed_=tax;
}

void Product::quantity(int q){
    quantity_=q;
}

void Product::qtyNeeded(int n){
    qtyNeeded_=n;
}

const char* Product::sku()const{
    return sku_;
}

const char* Product::name()const{
    return name_;
}

bool Product::taxed()const{
    return taxed_;
}

int Product::quantity()const{
    return quantity_;
}

int Product::qtyNeeded()const{
    return qtyNeeded_;
}

double Product::price()const{
    return price_;
}

bool Product::isEmpty()const{
    if(sku_==0 && sku_[0]==0 && name_==nullptr && price_==0&&taxed_==0 && quantity_==0&& qtyNeeded_==0)
        return true;
    else
        return false;
}

bool Product::operator==(const char* name){
    for(int i=0;name_[i]!=0;i++){
        if(name_[i]!=name[i])
            return false;
    }
    return true;
}

int Product::operator+=(int num){
    return (num+quantity_);
}

int Product::operator-=(int num){
    return (quantity_-num);
}

double Product::cost()const{
    if (taxed_){
        return price_;
    }
    else
        return price_;
}

double operator+=(double& num, const Product& P){
    return (num+=(P.price()*P.quantity()));
}

std::ostream& operator<<(std::ostream& os,const Product& P){
    P.write(os,P.taxed());
    return os;
}

std::istream& operator>>(std::istream& is, Product& P){
    P.read(is);
    return is;
}

}

最后是“主”文件

#include <iomanip>
#include <cstdlib>
#include "Product.h"

#ifdef TAB
# undef TAB
#endif

#define TAB '\t'

using namespace std;

namespace sict{

class SItem :public Product{

public:
    SItem(const char* theSku, const char * theName):Product(theSku, theName){}

    SItem(){}

    virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const{
            if (!isEmpty()){
            file.open("ms4.txt", ios::out|ios::app);
            file << sku() << TAB << name() << TAB << quantity() << TAB << qtyNeeded() << TAB << int(taxed()) << TAB << price() << endl;
            file.clear();
            file.close();
        }
        return file;
    }

    virtual std::fstream& load(std::fstream& file){
        file.open("ms4.txt", ios::in);
        char buf[2000];
        double dbuf;
        int ibuf;
        file >> buf;
        sku(buf);
        file >> buf;
        name(buf);
        file >> ibuf;
        quantity(ibuf);
        file >> ibuf;
        qtyNeeded(ibuf);
        file >> ibuf;
        taxed(bool(ibuf));
        file >> dbuf;
        price(dbuf);
        file.clear();
        file.close();
        return file;
    }

    virtual std::ostream& write(std::ostream& os, bool linear)const{
        return isEmpty() ? os : (os << sku() << ": " << name() << ", qty: " << quantity() << ", qtyNeeded:" << qtyNeeded() <<", Cost: " << fixed << setprecision(2) << cost());
    }

    virtual std::istream& read(std::istream& is){
        char buf[2000];
        double dbuf;
        int ibuf;
        cout << "Sku: ";
        is >> buf;
        sku(buf);
        cout << "Name (no spaces): ";
        is >> buf;
        name(buf);
        cout << "Qty: ";
        is >> ibuf;
        quantity(ibuf);
        cout << "Qty Needed: ";
        is >> ibuf;
        qtyNeeded(ibuf);
        cout << "Is taxed? (1/0): ";
        is >> ibuf;
        taxed(bool(ibuf));
        cout << "Price: ";
        is >> dbuf;
        price(dbuf);
        return is;
    }
};

}

void dumpFile(fstream& f){
    f.open("ms4.txt", ios::in);
    char ch;
    while (!f.get(ch).fail()){
        cout.put(ch);
    }
    f.clear();
    f.close();
    }

using namespace sict;

void test(){
    double res, val = 0.0;
    fstream F("ms4.txt", ios::out);
    F.close();
    SItem S;
    SItem T;
    SItem U;
    cout << "Enter Product info: " << endl;
    cin >> S;
    SItem V = S;
    S.store(F);
    T.load(F);
    cout << "T: (store, load)" << endl;
    cout << T << endl;
    cout << "S: " << endl;
    cout << S << endl;
    cout << "V(S): " << endl;
    cout << V << endl;
    cout << "U=T & op= :" << endl;
    U = T;
    cout << U << endl;
    cout << "Operator == :" << endl;
    cout << "op== is " << (T == "1234" ? "OK" : "NOT OK") << endl;
    cout << "op+=: " << endl;
    U += 10;
    cout << U << endl;
    cout << "op+=double : " << endl;
    res = val += U;
    cout << res << "=" << val <<endl;
}

int main(){
    fstream F("ms4.txt", ios::out);
    F.close();
    SItem S;
    SItem U("4321", "Rice");
    cout << "Empty Prouduct:" << endl << S << endl;
    cout << "U(\"4321\", \"Rice\"):" << endl << U << endl;

    cout << "Please enter the following information:" << endl;
    cout << "Sku: 1234" << endl;
    cout << "Name(no spaces) : Blanket" << endl;
    cout << "Qty : 12" << endl;
    cout << "Qty Needed : 23" << endl;
    cout << "Is taxed ? (1 / 0) : 1" << endl;
    cout << "Price : 12.34" << endl;
    test();
    cout << "Please enter the following information:" << endl;
    cout << "Sku: 1234" << endl;
    cout << "Name(no spaces) : Jacket" << endl;
    cout << "Qty : 12" << endl;
    cout << "Qty Needed : 23" << endl;
    cout << "Is taxed ? (1 / 0) : 0" << endl;
    cout << "Price : 12.34" << endl;
    test();
    dumpFile(F);
    cout << "----The End" << endl;
    return 0;
}

如果有兴趣的话,一般和可流传的文件:

#ifndef SICT_GENERAL_H__
#define SICT_GENERAL_H__
#define TAX (0.13)          
#define MAX_SKU_LEN (7)      
#define DISPLAY_LINES (10)     
#define MIN_YEAR (2000)        
#define MAX_YEAR (2030)
#define MAX_NO_RECS (2000)
#endif



#ifndef SICT__Streamable_H_
#define SICT__Streamable_H_
#include <iostream>
#include <fstream>
using namespace std;
namespace sict{
class Streamable{
public:
virtual fstream& store(fstream& file, bool addNewLine = true)const =0;
virtual fstream& load(std::fstream& file)=0;
virtual ostream& write(ostream& os, bool linear)const=0;
virtual istream& read(istream& is)=0;
};
}
#endif

程序在第一个delete语句崩溃,它甚至没有进入复制构造函数。我已经尝试了所有内容,但我不明白为什么它会在delete[]打破。

0 个答案:

没有答案