nnilnk 2015-05-25 12:20 采纳率: 0%
浏览 4920

c++初学者求助 Access violation reading location

函数如下

 #include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
 int main()
{
    int Month, Day;
    string Name, Email, Address;
    while (cin >> Month >> Day >> Name >> Email >> Address)
    {
        Person p(Month ,Day,Name,Email,Address), m;
        int size = sizeof(Person);
        string name = p.getName();
        int month = p.getMonth(), day = p.getDay();
        fstream out1("SortByName.dat", ios::out | ios::in | ios::binary);//按姓名排序
        out1.seekp(0, ios::end);
        while (out1.tellp())
        {
            out1.seekp(-size, ios::cur);
            out1.read((char*)&m, size);
            if (m.getName() <= name)
                break;
            out1.seekp(out1.tellp(), ios::beg);
            out1.write((const char*)&m, size);
            out1.seekp(-2 * size, ios::cur);

        }
        out1.write((const char*)&p, size);
        out1.close();
        fstream out2("SortByBirthDay.dat", ios::out | ios::in | ios::binary);//按生日排序
        out2.seekp(0, ios::end);
        while (out2.tellp())
        {
            out2.seekp(-size, ios::cur);
            out2.read((char*)&m, size);
            if ((m.getMonth() < month) || (m.getMonth() == month&&m.getDay() <= day))
                break;
            out2.seekp(out2.tellp(), ios::beg);
            out2.write((const char*)&m, size);
            out2.seekp(-2 * size, ios::cur);
        }
        out2.write((const char*)&p, size);
        out2.close();
    }
}

头文件Person.h

 //Person.h
 #ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<string>
using namespace std;

class Person
{
public:
    Person(int, int, string, string, string);
    Person(){};
    int getMonth() const;
    int getDay() const;
    string getName() const;
    void setEmail(string);
    void setTel(string);
    string getDayOfWeek() const;
    virtual void print() const;
protected:
    int month;
    int day;
    string m;
    string dayOfWeek;
    string name;
    string email;
    string tel;
};

#endif

Person.cpp在这

  #include"Person.h"

Person::Person(int a, int b, string c, string d, string e)
{
    month = a;
    day = b;
    name = c;
    email = d;
    tel = e;
    dayOfWeek = getDayOfWeek();
    string mm[12] = { "January", "February", " March", " April", " May", "June",
        "July", "August", "September", " October", " November", " December" };
    m = mm[month-1];
}
int Person::getMonth() const
{
    return month;
}

int Person::getDay() const
{
    return day;
}
string Person::getName() const
{
    return name;
}
void Person::setEmail(string a)
{
    email = a;
}
void Person::setTel(string a)
{
    email = a;
}
string Person::getDayOfWeek() const
{
    string week[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    int m=month,d=day,y=15;
    if (m < 3)
    {
        m += 12; y--;
    }
    int ans =  y + y / 4 + 26 * (m + 1) / 10 + d - 36;
    ans = (ans % 7 + 7) % 7;
    return week[ans];
}
void Person::print() const
{
    cout << m << '/' << day << '/' << dayOfWeek << "   " << name
        << "   Tel:" << tel << "   Email:" << email << endl;
}

整个函数运行结束后报错
Unhandled exception at 0x5527DF58 (msvcp120d.dll) in AddressBook.exe: 0xC0000005: Access violation reading location 0x0043E4A4.

求大神指导,在下感激不尽

  • 写回答

3条回答

  • 存钱买冰棍 2015-05-25 12:53
    关注

    0xC0000005错误 表示使用了未创建(未分配内存空间)的变量或对象。
    你看看是不是有些量没有初始化或者分配空间就用了

    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面