读取位置 0xcdcdcdc1 时发生访问冲突
调试时到了interface.cpp里的db->Append_back(info1);这一行的时候就提示“读取位置 0xcdcdcdc1 时发生访问冲突”
请帮我看看是怎么回事
//head.h#ifndef H_HEAD#define H_HEAD#include "database.h"#include "information.h"using namespace DATABASE;void console();#endif
//interface.cpp#ifndef H_INTERFACE#define H_INTERFACE#include "head.h"#include <stdio.h>#include <io.h>using namespace std;extern DataBase<Info>* db;const char* infos[] = {"请输入指令:\n", //0 "建立新的数据库\n", //1 "编辑数据库\n", //2 "打印数据库\n", //3 "浏览数据库\n", //4 "备份数据库\n", //5 "还原数据库\n", //6 "退出程序\n", //7 "下一条记录\n", //8 "上一条记录\n", //9 "添加新纪录\n", //10 "删除本条记录\n", //11 "编辑本条记录\n", //12 "返回到上一级\n", //13 "请按次输入:\n\t姓名\n\t性别\n\t年龄\n\t地址\n\t电话号码\n\t电话号码2\n\t以空格分开\n\t跳过的项目以两个以上个空格表示\n",//14 "数据库不存在\n" //15 };const char* OMf = "%n$%s$%a$%l$%p$%q#";const char* OMp = "%n\t%s\t%a\t%l\t%p\n";void console(){ char in = 0; /*if (!access("friends.dat",0)) { }*/ do { cout<< "---\t" << infos[0] << "1\t---\t" << infos[1] << "2\t---\t" << infos[2] << "3\t---\t" << infos[3] << "4\t---\t" << infos[4] << "5\t---\t" << infos[5] << "6\t---\t" << infos[6] <<"Q\\q\t---\t" << infos[7] << endl; if(cin >> in) { switch (in) { case '1': delete db; db = new DataBase<Info>; break; case '2': if (db == 0) { cout << infos[15] << endl; break; } do { cout<< "\t1\t---\t" << infos[8] << "\t2\t---\t" << infos[9] << "\t3\t---\t" << infos[10] << "\t4\t---\t" << infos[11] << "\t5\t---\t" << infos[12] << "\tB\\b\t---\t" << infos[13] << endl; cin >> in; switch(in) { case '1': db->Next(); break; case '2': db->Previous(); break; case '3': cout<< "\t---\t" << infos[14] << endl; { char n[12];char _s[3];bool s=Info::Male;int a;string l;char p[15];char q[15]; cin >> n >> _s >> a >> l >> p >> q; if (_s == "女") s = Info::Female; Info info1(n,s,a,l,p,q); db->Append_back(info1); } break; } }while(cin && in != 'B' && in != 'b'); break; case '4': if (db == 0) { cout << infos[15] << endl; break; } { IO<Info> io1(*db); io1.Output(cout,OMp); } break; case 'Q': return; case 'q': return; } } }while (cin);}#endif
//information.cpp#include "information.h"#include <string>void Info::Output(ostream& os,const char* omode){ int i = 0; while(omode[i] != '\0') { switch (omode[i]) { case '%': switch(omode[++i]) { case 'n': os << name; break; case 's': os << ((sex==Info::Male)?"男":"女"); break; case 'a': os << age; break; case 'l': os << location; break; case 'p': if (phone != 0) os << phone; break; case 'q': if (phone2 != 0) os << phone2; break; } break; default: os << omode[i]; break; } i++; }}Info::Info(const Info& info2):location(info2.location),age(info2.age),sex(info2.sex){ if (&info2 == this) return; delete[] phone; delete[] phone2; phone = new char[sizeof(info2.phone)/sizeof(char)]; phone2 = new char[sizeof(info2.phone2)/sizeof(char)]; int i = 0; while (info2.phone[i] != '\0') phone[i] = info2.phone[i++]; phone[i] = '\0'; i = 0; while (info2.phone2[i] != '\0') phone2[i] = info2.phone2[i++]; phone2[i] = '\0'; i = 0; while (info2.name[i] != '\0') name[i] = info2.name[i++]; name[i] = '\0';}Info::Info(char* n,bool s,int a,string l,const char* p,const char* p2):location(l),age(a),sex(s){ phone = new char[strlen(p)+1]; phone2 = new char[strlen(p2)+1]; int i = 0; while (p[i] != '\0') phone[i] = p[i++]; phone[i] = '\0'; i = 0; while (p2[i] != '\0') phone2[i] = p2[i++]; phone2[i] = '\0'; i = 0; while (n[i] != '\0') name[i] = n[i++]; name[i] = '\0';}//information.h#ifndef H_INFORMATION#define H_INFORMATION#include <string>#include <iostream>using namespace std;class Info{private: char name[12]; bool sex; int age; string location; char* phone; char* phone2;public: enum SEX{Male = true,Female = false}; explicit Info(){} explicit Info(char* n,bool s,int a,string l,const char* p=0,const char* p2=0); explicit Info(const Info& info2); const char* Getname(){return name;} bool Getsex(){return sex;} int Getage(){return age;} string& Getlocation(){return location;} const char* Getphone(){return phone;} const char* Getphone2(){return phone2;} void Output(ostream& os,const char* omode);};#endif//database.h#ifndef H_DATABASE#define H_DATABASE#include <iostream>#include <list>using namespace std;namespace DATABASE{template <class T>class DataBase{private: typedef typename list<T>::iterator Pointer; list<T> datas; Pointer pointer;public: DataBase():pointer(datas.begin()){} ~DataBase(){} //DataBase(DataBase& db2){} void Append_back(T& data){datas.push_back(data);} void Append_front(T& data){datas.push_front(data);} void Delete(){datas.erase(pointer);} void Modify(const T& data){pointer = data;} void Get(T& data){data = *pointer;} void GoTop(){pointer = datas.begin();} void GoBottom(){pointer = datas.end();} void Next(){pointer++;} void Previous(){pointer--;} bool eof(){return pointer == datas.end();} bool bof(){return pointer == datas.begin();}};template <class T>class IO{private: DataBase<T>& db;public: IO(DataBase<T>& d):db(d){} void Output(ostream& os,const char* omode); void Input();};template <class T>void IO<T>::Output(ostream& os,const char* omode){ T data; db.GoTop(); while(!db.eof()) { db.Get(data); data.Output(os,omode); db.Next(); }}}#endif
//main.cpp#include "head.h"DataBase<Info>* db = 0;int main(){ console(); delete db; return 0;}