当我尝试在 C++ 中编译和运行我的“main.cpp”文件时,导致调用中止方法的原因是啥?

Posted

技术标签:

【中文标题】当我尝试在 C++ 中编译和运行我的“main.cpp”文件时,导致调用中止方法的原因是啥?【英文标题】:What is causing the abort method to be called when I try to compile and run my "main.cpp" file in c++?当我尝试在 C++ 中编译和运行我的“main.cpp”文件时,导致调用中止方法的原因是什么? 【发布时间】:2020-07-22 18:48:32 【问题描述】:

我目前正在使用 C++ 开发一个项目,并且我有 3 个文件。 Customer.h、Customer.cpp 和 Main.cpp,每当我尝试运行 main.cpp 文件时,我都会收到一条错误消息并调用 abort 方法。我的 3 个文件中的任何代码都没有问题或任何其他错误消息,我不确定是什么导致了错误,非常感谢任何帮助!


“Main.cpp”的代码如下,因为我还不能上传任何图片

#include "Customer.h"
using namespace std;


int main()

    Customer cust1;

    cust1.setCustomerID(150032);
    cust1.setTitle("Mr");
    cust1.setName("Joey");
    cust1.setNumOfPurchases(3);
    cust1.setPurchases(366, 352, 334);
    cust1.setType("New");

    cout << cust1.getCustomerID() << endl;
    cout << cust1.getTitle() << endl;
    cout << cust1.getName() << endl;
    cout << cust1.getNumOfPurchases() << endl;
    cout << cust1.getPurchases() << endl;
    cout <<  cust1.getType() << endl;
   
    return 0;

来自下面 Customer.h 文件的代码

class Customer

private:
    int customerID;
    string title;
    string name;
    int numOfPurchases;
    int* purchases;
    string type;

public:
    Customer(); // default constructor
    Customer(int customerID, string title, string name, int numOfPurchases, int purchase1, int purchase2, int purchase3, string type);
    
    //copy overload assignment
    Customer& operator=(Customer& otherCustomer);
    Customer(const Customer& source);

    ~Customer(); //destructor

    //Getters and Setters
    void setCustomerID(int customerID);
    void setTitle(string title);
    void setName(string name);
    void setNumOfPurchases(int numOfPurchases);
    void setPurchases(int purchase1, int purchase2, int purchase3);
    void setType(string type);

    int getCustomerID();
    string getTitle();
    string getName();
    int getNumOfPurchases();
    int* getPurchases();
    string getType();

    void printCustomer() 
        cout << customerID << "," << title << "," << name << "," << numOfPurchases << "," << purchases << "," << type << endl;
    

    friend std::ostream& operator<<(std::ostream& out, Customer& customer); // overloaded operator<<
    friend istream& operator>> (istream& in, Customer& customer); // overloaded operator >>
;

来自下面 Customer.cpp 文件的代码

//default constructor
Customer::Customer() 



//Full constructor
Customer::Customer(int customerID, string title, string name, int numOfPurchases, int purchase1, int purchase2, int purchase3, string type) 

    customerID = customerID;
    title = title;
    name = name;
    numOfPurchases = numOfPurchases;
    purchases = new int[3];
    purchases[0] = purchase1;
    purchases[1] = purchase2;
    purchases[2] = purchase3;
    type = type;


Customer::Customer(const Customer& source) //copy constructor

    cout << "copy constructor called" << endl;
    this->customerID = source.customerID;
    this->title = source.title;
    this->name = source.name;
    this->numOfPurchases = source.numOfPurchases;
    this->purchases = new int[3];
    purchases[0] = source.purchases[0];
    purchases[1] = source.purchases[1];
    purchases[2] = source.purchases[2];
    this->type = source.type;


//overloaded assignment operator=
Customer& Customer::operator= (Customer& otherCustomer)

    cout << "Overloaded assignment operator= called" << endl;

    //self-assignment guard
    if (this == &otherCustomer)
        return *this;  //refernce to the same object

   // copy data from the source (rhs) to this object (the destination)
    name = otherCustomer.name;

    //must make a new scores object to store a copy of the other student 
    if (purchases != nullptr)
        delete[] purchases;

    purchases = new int[3];
    for (int i = 0; i < 3; i++) 
        purchases[i] = otherCustomer.purchases[i];
    

    //return this existing object so we can chain this operator 
    return *this;

Customer::~Customer() 
    cout << "Destructor ~Customer called" << endl;
    delete[] purchases;


// Overloaded insertion operator  (Outputs Character object data as an output stream)
// Defined in header file as a  "friend" function, as it is not a member function
//
ostream& operator<<(ostream& out, Customer& customer)

    cout << "Customer details ( output by insertion operator<< )" << endl;
    cout << "Customer ID: " << customer.customerID << endl;
    cout << "Title: " << customer.title << endl;
    cout << "Name: " << customer.name << endl;
    cout << "Number of purchases: " << customer.numOfPurchases << endl;
    cout << "Purchases: ";
    for (int i = 0; i < 3; i++)
    
        if (i > 0) cout << ",";
        cout << customer.purchases[i];
    
    cout << "Type: " << customer.type << endl;
    
    return out;



istream& operator>> (istream& in, Customer& customer)

    cout << "Enter Customer details ( using the extraction operator>> )" << endl;
    cout << "Enter Customer ID: " << endl;
    cin >> customer.customerID;
    cout << "Enter Title: " << endl;
    getline(cin, customer.title);
    cout << "Enter Name: " << endl;
    getline(cin, customer.name);
    cout << "Enter Number of Purchases: ";
    cin >> customer.numOfPurchases; 
    cout << "Enter Purchases: ";
    cin >> customer.purchases[0];
    cin >> customer.purchases[1];
    cin >> customer.purchases[2];
    cout << "Enter Type";
    getline(cin, customer.type);
    cout << endl;

    return in;


int Customer::getCustomerID()

    return customerID;


string Customer::getTitle()

    return title;


string Customer::getName()

    return name;


int Customer::getNumOfPurchases()

    return numOfPurchases;


int* Customer::getPurchases()

    return purchases;


string Customer::getType()

    return type;


void Customer::setCustomerID(int customerID)

    if (customerID < 1) 
        throw invalid_argument("Customer ID has to be equal to 1 or more");
    
    this->customerID = customerID;


void Customer::setTitle(string title)

    if (title.length() < 2) 
        throw invalid_argument("Title has to be more than or equal to 2 characters");
    
    this->title = title;


void Customer::setName(string name)

    if (name.length() < 4) 
        throw invalid_argument("Length of name should be more than or equal to 4 characters");
    
    this->name = name;


void Customer::setNumOfPurchases(int numOfPurchases)

    if(numOfPurchases > 0 && numOfPurchases < 10000)
        throw invalid_argument("Number of purchases should be between 0 to 10000");
    
    this->numOfPurchases = numOfPurchases;


void Customer::setPurchases(int purchase1, int purchase2, int purchase3)

    if (purchase1 < 0 || purchase2 < 0 || purchase3 < 0) 
        throw invalid_argument("Purchases must be more than or equal to zero");
    


void Customer::setType(string type) 
    if (type != "New" || type != "Either") 
        throw invalid_argument("Type of purchase has to be New or Either");
    
 

【问题讨论】:

我们需要查看Customer类的内容才能完全理解你的代码。 不要上传图片,上传代码。您缺少的最重要的东西是您的编译命令。此外,Customer.hCustomer.cpp 文件可能会有所帮助。 欢迎来到 Stack Overflow!请将您的问题editminimal reproducible example 或SSCCE (Short, Self Contained, Correct Example) 感谢您的反馈!我现在已经对其进行了编辑,并从我的其他两个文件中添加了代码! :) 程序中止时正在执行哪一行? 【参考方案1】:

您缺少一些指令(命名空间 std、iostream 等),但我已修复该问题并将问题重现到此处:

libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: Number of purchases should be between 0 to 10000 Abort trap: 6

使用您提供的 Customer.cpp 代码(感谢添加),我发现您在第 161 行有一个逻辑错误

void Customer::setNumOfPurchases(int numOfPurchases)

    if(numOfPurchases > 0 && numOfPurchases < 10000)
        throw invalid_argument("Number of purchases should be between 0 to 10000");
    
    this->numOfPurchases = numOfPurchases;

显然,如果 numOfPurchases 介于 0 和 1000 之间,则会根据您的设计引发 invalid_argument 错误。你应该把它改成这样:

void Customer::setNumOfPurchases(int numOfPurchases)

    if(numOfPurchases < 0 || numOfPurchases > 10000)
        throw invalid_argument("Number of purchases should be between 0 to 10000");
    
    this->numOfPurchases = numOfPurchases;

修复发现另一个错误:

libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: Type of purchase has to be New or Either Abort trap: 6

这导致我进入第 176 行:

void Customer::setType(string type) 
    if (type != "New" || type != "Either") 
        throw invalid_argument("Type of purchase has to be New or Either");
    
 

啊,经典的字符串比较问题。这不是在 C++ 中比较字符串的方式。改用字符串库中的compare 方法尝试类似的方法。

void Customer::setType(string type) 
    if (type.compare("New") != 0 && type.compare("Either") != 0) 
        throw invalid_argument("Type of purchase has to be New or Either");
    
 

这些更改为我解决了问题,并且运行良好。另外,我询问了您使用的编译命令,似乎没有提供,所以我只使用了g++ main.cpp

【讨论】:

非常感谢您的帮助!这是我第一次在这个网站上提出问题,所以我不确定如何上传代码,但我想我现在有了,哈哈,我非常感谢有关如何提出问题的反馈,当然还有你的回答! 我使用 Visual Studio 构建和编写我的代码,因此它使用 Microsoft C++ (MSVC) 编译器,代码仍然无法正常工作,并且当我尝试运行我的代码时,我不断收到中止消息,即使我进行了您在上面给我的更改 @Aisling 如果此解决方案有帮助,请投票并标记为答案,以便将来也可以帮助其他人 没有,我仍然收到中止消息 您修复了我上面提到的Customer.cpp 中的Customer::setNumOfPurchasesCustomer::setType 函数?

以上是关于当我尝试在 C++ 中编译和运行我的“main.cpp”文件时,导致调用中止方法的原因是啥?的主要内容,如果未能解决你的问题,请参考以下文章

尝试在 Windows 上执行 C++ 时出错(通过 MinGW 编译)

“你好世界!”在 C++ 中冻结

Makefile - 用于C程序的Unix

通过 C++ 运行外部 .exe

运行 C++ 代码时出现分段错误

在 C++ 中读取顺序文件时出错