显示“抛出'std :: bad_alloc'实例后调用终止时出错”

时间:2016-05-16 06:20:02

标签: c++

当我运行此程序时,它会出现致命错误并且程序已停止。 我认为这个问题是由于字符串类型可能正在使用std :: string。因为当我使用char数据类型时它运行正常。有什么问题可以指导。

parcel.cpp

#include "Parcel.h"
#include <iostream>
#include <string>

using namespace std;
bool check = false;

// Constructor
Parcel::Parcel(int id, std::string senderName, std::string senderAddress, std::string receiverName, std::string receiverAddress, int weight, int fee){
    this->id = id;
    this->senderName = senderName;
    this->senderAddress = senderAddress;
    this->receiverName = receiverName;
    this->receiverAddress = receiverAddress;
    this->weight = weight;
    this->fee = fee;
};

// Destructor
Parcel::~Parcel() {
    cout << "Destructor called";
}

// Defination of setter
void Parcel::setID(int id) {
    this->id = id;
    if(id < 0) {
        cout << endl << "Error: Please write valid receipt number e.g. 0 to onward";
    }
}
void Parcel::setWeight(int weight) {
    this->weight = weight;
    if(weight < 0) {
        cout << endl << "Error: Please write valid weight e.g. above to 0 grams";
    }
}
void Parcel::setFee(int fee) {
    this->fee = fee;
    if(fee < 0) {
        cout << endl << "Error: Please write valid fee e.g. 0 to onward";
    }
}
void Parcel::setSenderName(std::string senderName) {
    this->senderName = senderName;
//  if(strlen(senderName) == 0) {
//      cout << endl << "Error: Please write sender name";
//  }
}
void Parcel::setSenderAddress(std::string senderAddress) {
    this->senderAddress = senderAddress;
//  if(senderAddress == 0) {
//      cout << endl << "Error: Please write sender address";
//  }
}
void Parcel::setReceiverName(std::string receiverName) {
    this->receiverName = receiverName;
//  if(receiverName == 0) {
//      cout << endl << "Error: Please write reciever name";
//  }
}
void Parcel::setReceiverAddress(std::string receiverAddress) {
    this->receiverAddress = receiverAddress;
//  if(receiverAddress == 0) {
//      cout << endl << "Error: Please write reciever address";
//  }
}

// Defination of getter 
int Parcel::getID() {
    return id;
}
int Parcel::getWeight() {
    return weight;
}
int Parcel::getFee() {
    return fee;
}
string Parcel::getSenderName() {
    return senderName;
}
string Parcel::getSenderAddress() {
    return senderAddress;
}
string Parcel::getReceiverName() {
    return receiverName;
}
string Parcel::getReceiverAddress() {
    return receiverAddress;
}

parcel.h

#ifndef PARCEL_H
#define PARCEL_H
#include <iostream>
#include <string>

class Parcel
{
    private:
        // Declare data members
        int id, weight, fee;
        std::string senderName, senderAddress, receiverName, receiverAddress;           
    public:
        // Setter function
        void setID(int id);
        void setWeight(int weight);
        void setFee(int fee);
        void setSenderName(std::string senderName);
        void setSenderAddress(std::string senderAddress);
        void setReceiverName(std::string receiverName);
        void setReceiverAddress(std::string receiverAddress);

        // getter function
        int getID();
        int getWeight();
        int getFee();
        std::string getSenderName();
        std::string getSenderAddress();
        std::string getReceiverName();
        std::string getReceiverAddress();

        // Constructor
        Parcel(int id, std::string senderName, std::string senderAddress, std::string receiverName, std::string receiverAddress, int weight, int fee);

        // Destructor
        ~Parcel();

    protected:
};

#endif

的main.cpp

#include <iostream>
#include "Parcel.h"
#include <string>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

// Data member to take input from user
int id, weight, fee;
std::string senderName, senderAddress, receiverName, receiverAddress;

// main function
int main(int argc, char** argv) {

    cout << "Please enter receipt  number: ";
    cin >> id;
    cout << "Please enter sender name: ";
    cin >> senderName;
    cout << "Please enter sender address: ";
    cin >> senderAddress;
    cout << "Please enter receiver name: ";
    cin >> receiverName;
    cout << "Please enter receiver address: ";
    cin >> receiverAddress;
    cout << "Please enter parcel weight(gms): ";
    cin >> weight;
    cout << "Please enter parcel fee: ";
    cin >> fee;

    // Instance of Parcel class
    Parcel parcel(parcel.getID(), parcel.getSenderName(), parcel.getSenderAddress(), parcel.getReceiverName(), parcel.getReceiverAddress(), parcel.getWeight(), parcel.getFee());

    // Set values
    parcel.setID(id);
    parcel.setSenderName(senderName);
    parcel.setSenderAddress(senderAddress);
    parcel.setReceiverName(receiverName);
    parcel.setReceiverAddress(receiverAddress);
    parcel.setWeight(weight);
    parcel.setFee(fee);

    // Make output to show on console
    cout << endl << endl;
    cout << "Shipment Receipt" << endl << "___________________________________________" << endl;
    cout << "Receipt No.: " << parcel.getID() << endl << "Sender Name: " << parcel.getSenderName() << endl 
        << "Sender Address: " << parcel.getSenderAddress() << endl << "Receiver Name: " << parcel.getReceiverName() << endl
        << "Receiver Address: " << parcel.getReceiverAddress() << endl << "Parcel Weight: " << parcel.getWeight() << endl 
        << "Parcel Shipping Charges: " << parcel.getFee() << endl;

    // Destructor call
    parcel.~Parcel();
}

3 个答案:

答案 0 :(得分:3)

  

Parcel parcel(parcel.getID(), parcel.getSenderName(), parcel.getSenderAddress(), parcel.getReceiverName(), parcel.getReceiverAddress(), parcel.getWeight(), parcel.getFee());

不要那样做。在parcel.getSenderName()对象完全初始化之前,您正在调用parcel

答案 1 :(得分:1)

当我尝试运行时,您的程序不会以错误的alloc实例终止。但是,由于此行

,它确实会收到警告

Parcel parcel(parcel.getID(), parcel.getSenderName(), parcel.getSenderAddress(), parcel.getReceiverName(), parcel.getReceiverAddress(), parcel.getWeight(), parcel.getFee());

您应该考虑默认构造函数Parcel(),其默认值分配给不同的属性,然后Parcel parcelmain()

中分配它们之前

你用什么参数运行你的程序?

答案 2 :(得分:1)

你的建筑,

 Parcel parcel(parcel.getID(), parcel.getSenderName(), parcel.getSenderAddress(), parcel.getReceiverName(), parcel.getReceiverAddress(), parcel.getWeight(), parcel.getFee());

用自己初始化parcel,这是未初始化的 这是未定义的,任何事情都可能发生。

您已经阅读了用户的所有参数,因此请使用它们:

Parcel parcel(id, senderName, senderAddress, receiverName, receiverAddress, weight, fee);

您可以删除整个setter序列。

你不应该叫析构函数; C ++自动处理破坏 如果你在一本书中找到了这个想法,立即扔掉它并寻找一个更好的here 如果您没有书,请查看相同的地方。