Pointers 需要帮助找出正确的语法来更新全局范围内的对象吗
我有以下代码:Pointers 需要帮助找出正确的语法来更新全局范围内的对象吗,pointers,scope,iterator,global,local,Pointers,Scope,Iterator,Global,Local,我有以下代码: int do_transact(ifstream & inFile, list<shared_ptr<Bike>> bikelist, status s) { int id, i = 0, size = bikelist.size(); float days; string str, str2; char name[50]; list<shared_ptr<Bike>>::iterat
int do_transact(ifstream & inFile, list<shared_ptr<Bike>> bikelist, status s)
{
int id, i = 0, size = bikelist.size();
float days;
string str, str2;
char name[50];
list<shared_ptr<Bike>>::iterator it = bikelist.begin();
if (s == NO_STATUS) //performs rental
{
inFile >> id;
inFile >> days;
inFile >> str;
inFile >> str2;
strcpy_s(name, str.c_str());
while (i < size)
{
if (id == (*it)->id_num) // rents bike
{
cout << "vvvvvv PERFORMING RENTAL vvvvvv" << endl << endl;
cout << "The price of this rental will be: $" << (days)*((*it)->cost_per_day) << endl;
strcpy_s((*it)->to_whom, name);
(*it)->rented_code = RENTED;
cout << "Thank you for your business!" << endl << endl;
return 0;
}
i++;
it++;
}
}
}
int do_transact(ifstream&infle,列表bikelist,状态s)
{
int-id,i=0,size=bikelist.size();
浮动天数;
字符串str,str2;
字符名[50];
list::iterator it=bikelist.begin();
如果(s==无状态)//执行租赁
{
infle>>id;
填充>>天;
infle>>str;
infle>>str2;
strcpy_s(名称,str.c_str());
而(iid\u num)//
{
请提供一个。我唯一看到的是bikelist
是通过值传递的,而不是通过引用传递的,因此创建了列表的副本,但是由于列表只有指针,这不会影响结果,因为代码没有修改bikelist
本身。通过值传递只是错过了优化。代码正在访问o原始列表所指向的原始Bike
对象。请调试您的代码,以确保1)status
实际上是NO_status
,2)bikelist
不是空的,3)infle
包含您期望的输入数据。我无法使用您显示的代码重现问题。ori只要所有输入正确,ginal列表对象将按预期更新。