我有一个visual c++程序出了错误,不知为什么会出错?
我有以下小的visual c++程序:
#include <iostream>
#include <string>
using namespace std;
#include "list"
#include "algorithm"
class person
{
public:
char *name;
int age;
public:
person()
{
}
person(char *name,int age)
{
this->name=name;
this->age=age;
}
printinfo()
{
cout << "name=" << this->name <<",age=" << this->age << endl;
}
};
void main()
{
person p1("lili",25),p2("wuwu",23),p3("xiaoxiao",22),p4("bigbig",26),p5("xiaowu",22);
person *tp1,*tp2,*tp3,*tp4,*tp5,*tp6,*tp7,*tp8;
tp1=&p1;
tp2=&p2;
tp3=&p3;
tp4=&p4;
tp5=&p5;
list<person *> v2;
v2.push_back(tp1);
v2.push_back(tp2);
v2.push_back(tp3);
v2.push_back(tp4);
v2.push_back(tp5);
person p6("bigwu",28),p7("bigli",27),p8("xiaoli",20);
tp6=&p6;
tp7=&p7;
v2.push_back(tp6);
v2.push_back(tp7);
list<person *>::iterator lv2=v2.begin();
lv2++;
lv2++;
lv2++;
v2.insert(lv2,tp8);
for(list<person *>::iterator it=v2.begin();it!=v2.end();++it)
{
(*it)->printinfo();
}
}
运行时出错了,但我把v2.insert(lv2,tp8);这句话前面加了//以后,就不会出错了,便我需要在第三个位置上插入tp8这个元素就没有实现了,请各位高手指教!