当我尝试在 C++ 中编译和 运行 我的 "main.cpp" 文件时,是什么导致调用 abort 方法?
What is causing the abort method to be called when I try to compile and run my "main.cpp" file in c++?
我目前正在用 C++ 开发一个项目,我有 3 个文件。 Customer.h、Customer.cpp 和 Main.cpp,每当我尝试 运行 main.cpp 文件时,我都会收到一条错误消息并调用中止方法。我的 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");
}
}
您遗漏了一些指令(命名空间 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");
}
}
啊,经典的字符串比较问题。这是 NOT 在 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
.
我目前正在用 C++ 开发一个项目,我有 3 个文件。 Customer.h、Customer.cpp 和 Main.cpp,每当我尝试 运行 main.cpp 文件时,我都会收到一条错误消息并调用中止方法。我的 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");
}
}
您遗漏了一些指令(命名空间 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");
}
}
啊,经典的字符串比较问题。这是 NOT 在 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
.