C++的新手,程序在取消引用向量的一部分时没有响应



我昨天学习了C++,我正在努力解决USACO的训练问题。http://train.usaco.org/usacoprob2?a=iKSzALidh4Q&S=gift1

对于这个,我创建了一个People指针的向量。然而,经过一些故障排除,我发现当我尝试做一些类似的事情时

Person bob = *(people.at(i));

people.at(i) -> setbalance(giveself); // giveself is an int

程序没有响应,并且:进程已终止,状态为1073741819(0分钟3秒)。

我也是这个论坛的新手。这是我的代码:包括报表

using namespace std;
class Person
{
private:
int balance;
int origbalance;
string name;
public:
int getbalance() {return balance;}
string getname() {return name;}
void setbalance(int b){balance +=b;}
void setorigbalance(int o) {origbalance = o;}
int getorigbalance() {return origbalance;}
void giveTo(int num, Person* y) {y->setbalance(num);}
~Person();
Person(string n);
};
Person::Person(string n)
{
name = n;
}
Person::~Person()
{
}
int main()
{
ofstream fout ("gift1.out");
ifstream fin ("gift1.in");
int NP;
fin>>NP;
cout<<NP<<endl;
vector<Person*> people(NP);
cout<<"Created vectorn"<<endl;
for(int i = 0; i<NP; i++)
{
string nam;
fin>>nam;
Person* p = new Person(nam);
people.push_back(p);
cout<<nam<<endl;
}
cout<<"nFilled vector, size = "<<people.size()<<endl;
for(int i = 0; i<NP; i++)
{
string temp;
fin>>temp;
cout<<"nNow receiving "<<temp<<endl;
int togive, numgiving;
fin>>togive>>numgiving;
cout<<"n"<<temp<<" is dividing "<<togive<<" among "<<numgiving<<"     people"<<endl;
Person bob = *(people.at(i));
cout<<"hi bob"<<endl;
//(*people.at(i)).setorigbalance(togive);
cout<<"Original balance set"<<endl;
int giveeach = togive/numgiving;
cout<<"or "<<giveeach<<" to each person"<<endl;
int giveself = togive%numgiving;
cout<<"and "<<giveself<<" to himself :/"<<endl;
people.at(i) -> setbalance(giveself);
for(int j=0; j<numgiving; j++)
{
string nametogiveto;
fin>> nametogiveto;
cout<<nametogiveto<<endl;
for(int k=0; k<NP; k++)
{
string namy = people.at(k)->getname();
if(namy==nametogiveto)
{
cout<<"nHere you go "<<namy<<" have "<<giveeach<<endl;
people.at(k)->setbalance(giveeach);
people.at(i)->setbalance(-giveeach);
break;
}
}
}
}
for(int i=0; i<NP; i++)
{
cout<<people.at(i)->getname()<<endl;;
cout<<people.at(i)->getorigbalance() - people.at(i)->getbalance()<<endl;
cout<<endl;
fout<<people.at(i)->getname();
fout<<people.at(i)->getorigbalance() - people.at(i)->getbalance()<<endl;
}
return 0;
}

由于取消引用空指针,您遇到了未定义的行为

vector<Person*> people(NP);

此行创建具有NP空指针的矢量。稍后添加实际指针,但只访问作为空指针的第一个NP元素。

也就是说,你甚至不需要这里的指针,我建议你去掉它们。事实上,由于使用new分配指针,但从未对其调用delete,因此会出现内存泄漏。根据我的经验,刚开始使用C++的人通常会过度使用指针,所以首先要想办法避免它们。

更改

vector<Person*> people(NP);

vector<Person> people;

并使用填充

for(int i = 0; i<NP; i++)
{
string nam;
fin>>nam;
Person p(nam); // no more need for pointer or new here
people.push_back(p);
cout<<nam<<endl;
}

以后访问它时,您也不需要任何取消引用。这意味着你可以去掉所有的*,例如:

Person bob = *(people.at(i));

转换为:

Person bob = people.at(i);

您可以在任何地方使用.而不是->访问成员函数,例如:

people.at(k)->setbalance(giveeach);

会变成:

people.at(k).setbalance(giveeach);

这意味着消除了许多不必要的指针去引用,也消除了以前可能发生的内存泄漏。

对我来说,由于打开输入文件fin不成功,您似乎收到了这个错误。要进行检查,请在定义fin变量后添加以下行:

ifstream fin ("gift1.in");
if(!fin) {
cout << "Error opening input file.n";
}

在任何情况下,检查文件是否成功打开始终是一种很好的做法。

最新更新