2 nnilnk nnilnk 于 2015.05.26 20:12 提问

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在这

//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.

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

5个回答

bdmh
bdmh   Ds   Rxr 2015.05.26 21:51

指针飞了,看看中途是否改变过指针指向

91program
91program   Ds   Rxr 2015.05.27 07:38

请问:这段程序,是输入任意一组数据都会报错?还是要特定的输入数据才会报错?

nnilnk
nnilnk 回复91program: 找到哪里错了,Person类里的string数据改成char数组就可以了
2 年多之前 回复
91program
91program 回复nnilnk: 第一组数据,任意的数据输入都会报错?如果是,则可以单步调试一下看看是哪一句出错了,再对此句进行分析。
2 年多之前 回复
nnilnk
nnilnk 只能输入一组数据,输入第二组就会报错
2 年多之前 回复
VisualEleven
VisualEleven   Ds   Rxr 2015.05.27 09:52

Debug下打开call stack,看看函数调用堆栈,先找到出错的函数调用

xy707707
xy707707   2015.05.27 17:15

非法访问,,引起的错误

u012377333
u012377333   Rxr 2015.05.28 19:17

非法访问数据,这个问题要注意主机类型的和访问的地址

Csdn user default icon
上传中...
上传图片
插入图片