感谢您查看我的问题。
具体问题是:我在这里做错了什么,导致程序崩溃?
我正在做一个项目,该项目接受客户的订单、存储他们的个人数据并存储他们的最后一个订单。
我上了两节课。客户和订单。
客户包含一个订单数组(在对向量进行故障排除之前)。
以下是我项目中的代码。
从我的 ohistory.h 文件中:
class Order
{
public:
Order(string* oERROR= new string(""));
~Order();
bool SetOrder(const string& main, const string& drink, const string& side);
void GetOrder() const;
void GetError() const;
string GetMain() const;
string GetSide() const;
string GetDrink() const;
private:
string m_main;
string m_drink;
string m_side;
string* mORDER_ERROR;
};
class Customer
{
public:
Customer(string* cERROR = new string(""));
~Customer();
void GetName() const;
bool SetName(const string &fName, const string& lName);
bool SetAddress(const string& Addr);
bool SetPhone(const string & pNumber);
void GetOrder(const int& numOrder) const;
bool SetOrder(const int& numOrder);
void GetError() const;
private:
string m_firstName;
string m_lastName;
string m_Address;
string m_phoneNumber;
Order m_cOrder[10];
string* pCUS_ERROR;
Order askOrder();
};
ohistory.cpp 中的相关条目
string Order::GetMain() const
{
string temp = m_main;
return temp;
}
string Order::GetDrink() const
{
string temp = m_drink;
return temp;
}
string Order::GetSide() const
{
string temp = m_side;
return temp;
}
Order Customer::askOrder()
{
string main, side, drink;
Order newOrder;
cout << "***\tEnter a main item: ";
getline(cin, main);
cout << "\n***\t\tEnter a side item: ";
getline(cin,side);
cout << "\n***\t\tEnter a drink: ";
getline(cin,drink);
newOrder.SetOrder(main, drink, side);
return newOrder;
}
bool Customer::SetOrder(const int& numOrder)
{
for (int i = 0; i < numOrder; ++i)
{
Order temp = askOrder();
m_cOrder[i].SetOrder(temp.GetMain(), temp.GetDrink(), temp.GetSide());
}
return true;
}
从 main.cpp(对 customer.setOrder() 的调用在底部,main 调用 newCustomer())
void newCustomer()
{
cin.clear();
char correctCustomerInfo;
char correctOrder;
Customer newCustomer;
string first_name, last_name, address, phone_number;
do
{
cout << "***\tEntering new customer:\n\n";
cout << "***\t\tFirst Name:";
getline(cin, first_name);
cout << "***\t\tLast Name: ";
getline(cin, last_name);
cout << "***\t\tAddress: ";
getline(cin, address);
cout << "***\t\tPhone Number: ";
getline(cin, phone_number);
cout << endl;
newCustomer.SetName(first_name, last_name);
newCustomer.SetAddress(address);
newCustomer.SetPhone(phone_number);
newCustomer.GetName();
correctCustomerInfo = askQuestion("Is the user's information correct?");
} while (correctCustomerInfo != 'y');
int choice;
cout <<"***\tPlease enter order:\n\n";
cout <<"***\tTotal number of Orders:\n";
cin >> choice;
cin.clear();
cin.ignore(INT_MAX, '\n');
do
{
newCustomer.SetOrder(choice);
correctOrder = askQuestion("Is this the correct order?");
} while (correctOrder != 'y');
}
运行调试时从 VC++ 转储:
'OrderTaker.exe': Loaded 'C:\Documents and Settings\*\My Documents\Visual Studio 2010\Projects\OrderTaker\Debug\OrderTaker.exe', Symbols loaded.
'OrderTaker.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Cannot find or open the PDB file
'OrderTaker.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', Cannot find or open the PDB file
'OrderTaker.exe': Loaded 'C:\WINDOWS\system32\msvcp100d.dll', Symbols loaded.
'OrderTaker.exe': Loaded 'C:\WINDOWS\system32\msvcr100d.dll', Symbols loaded.
First-chance exception at 0x103157aa (msvcr100d.dll) in OrderTaker.exe: 0xC0000005: Access violation reading location 0xfeeefee2.
Unhandled exception at 0x103157aa (msvcr100d.dll) in OrderTaker.exe: 0xC0000005: Access violation reading location 0xfeeefee2.
pUserData 0xfeeefeee void *
Head 0xfeeefece {pBlockHeaderNext=??? pBlockHeaderPrev=??? szFileName=??? ...} _CrtMemBlockHeader *
pBlockHeaderNext CXX0030: Error: expression cannot be evaluated
pBlockHeaderPrev CXX0030: Error: expression cannot be evaluated
szFileName CXX0030: Error: expression cannot be evaluated
nLine CXX0030: Error: expression cannot be evaluated
nDataSize CXX0030: Error: expression cannot be evaluated
nBlockUse CXX0030: Error: expression cannot be evaluated
lRequest CXX0030: Error: expression cannot be evaluated
gap 0xfeeefeea <Bad Ptr> unsigned char [4]
[0] CXX0030: Error: expression cannot be evaluated
[1] CXX0030: Error: expression cannot be evaluated
[2] CXX0030: Error: expression cannot be evaluated
[3] CXX0030: Error: expression cannot be evaluated
问题发生在:
void Customer::SetOrder(const int& numOrder)
{
for (int i = 0; i < numOrder; ++i)
{
Order temp = askOrder();
m_cOrder[i].SetOrder(temp.GetMain(), temp.GetDrink(), temp.GetSide());
}
}
我能够输入 3 个项目,然后它崩溃了。
你可能会注意到一些奇怪的返回类型,那是我为我计划在未来做的一些步骤做准备。
提前致谢。
================================编辑
我想出了我的问题。感谢icepack 把我送到了正确的方向。
无论哪种方式,以防有人有类似的问题。
问题出在我收到的用户输入上。
int choice;
cout <<"***\tPlease enter order:\n\n";
cout <<"***\tTotal number of Orders:\n";
cin >> choice;
cin.clear();
cin.ignore(INT_MAX, '\n');
do
{
newCustomer.SetOrder(choice);
correctOrder = askQuestion("Is this the correct order?");
} while (correctOrder != 'y');
我没有将用户输入直接发送给类及其数据成员,而是接受输入,访问其值,然后重新为其分配相同的值。
从那里开始工作就像一个魅力。