菜鸟求教,实在不知道哪里错了,编译没错,运行后有错误!!!

kitty_ling0921 2012-05-06 07:23:05
请教,编译通过了,以下程序运行后会有错误:
C++ project.exe 中的 0x5986ad4a (msvcp100d.dll) 处有未经处理的异常: 0xC0000005: 读取位置 0x001038f4 时发生访问冲突

位置在output_employee函数结束时有错误,但具体原因不知。。。 我是新手,望各位指点迷津,谢谢大家!

//source program:

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

class Employee
{
public:
Employee(){num=0;name="x";age=1;salary=0.0;}
Employee(int n,string na,int a,int s):num(n),name(na),age(a),salary(s){}
friend ostream &operator<<(ostream &,Employee &);
void getvalue()
{
cout<<"Please input the data."<<endl;
cin>>num>>name>>age>>salary;
}
friend void sort(Employee*,int);
private:
int num;
string name;
int age;
int salary;
};

void filewrite(char *fname,Employee *pE)
{
ofstream outfile(fname,ios::binary);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<5;i++)
outfile.write((char*)&pE[i],sizeof(pE[i]));
outfile.close();
}

void sort(Employee *pE,int count)
{
Employee eTemp;
int ePos;
for(int i=0;i<count;i++)
{
eTemp=pE[i];
ePos=i;
for(int j=i+1;j<count;j++)
{
if(pE[j].num<eTemp.num)
{
eTemp=pE[j];
ePos=j;
}
}
pE[ePos]=pE[i];
pE[i]=eTemp;
}
}

ostream& operator<<(ostream& output,Employee& e2)
{
output<<"num:"<<e2.num<<" name:"<<e2.name<<" age:"<<e2.age<<" salary:"<<e2.salary<<endl;
return output;
}

void output_employee(char* fname)
{
Employee aemp[5];
ifstream infile(fname,ios::binary);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<5;i++)
infile.read((char*)&aemp[i],sizeof(aemp[i]));
infile.close();
cout<<"Here is the data:"<<endl;
for(int i=0;i<5;i++)
{
cout<<aemp[i];
}
}

int main()
{
Employee emp[5]={
Employee(2,"B",12,22),
Employee(5,"E",15,55),
Employee(1,"A",11,11),
Employee(4,"D",14,14),
Employee(3,"C",13,13)
};

sort(emp,5);
filewrite("employee.dat",emp);

output_employee("employee.dat");

return 0;
}
...全文
300 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
kitty_ling0921 2012-05-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

引用 6 楼 的回复:

LZ,在CODEBLOCKS环境下验证没问题


恩,谢谢你提供的信息。我用的是VS2010,这个环境下可能会有问题
[/Quote]

谢了。
laurent1987 2012-05-06
  • 打赏
  • 举报
回复
F11跳到子函数里,F10单步运行,直到蹦到出错的那个函数为止。。
我给你改的函数写的不咋地,你自己再完善下就好了。
kitty_ling0921 2012-05-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

LZ,在CODEBLOCKS环境下验证没问题
[/Quote]

恩,谢谢你提供的信息。我用的是VS2010,这个环境下可能会有问题
kitty_ling0921 2012-05-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

参考:http://topic.csdn.net/u/20100730/12/71cf86d7-04ca-446c-b931-6c83d47d4c19.html

楼主要学会根据报的错查找资料哈~

string转换成为char*出现的错误

[/Quote]

谢谢你! 你真厉害,确实是这个问题,还想请教一下你是如何查到那个帖子的。 能不能指导一下如何学会根据报的错查找资料?Thanks.
kakaximodo 2012-05-06
  • 打赏
  • 举报
回复
LZ,在CODEBLOCKS环境下验证没问题
laurent1987 2012-05-06
  • 打赏
  • 举报
回复
参考:http://topic.csdn.net/u/20100730/12/71cf86d7-04ca-446c-b931-6c83d47d4c19.html

楼主要学会根据报的错查找资料哈~

string转换成为char*出现的错误


class Employee
{
public:
Employee(){num=0;name='\0';age=1;salary=0;}
Employee(int n,char* na,int a,int s):num(n),age(a),salary(s)
{
int len = sizeof(na) +1;
name = new char [len];
name = na;
}
friend ostream &operator<<(ostream &,Employee &);
void getvalue()
{
cout<<"Please input the data."<<endl;
cin>>num>>name>>age>>salary;
}
friend void sort(Employee*,int);
private:
int num;
// string name;
char* name;
int age;
int salary;
};

问题解决
kitty_ling0921 2012-05-06
  • 打赏
  • 举报
回复
这个是输出

“C++ project.exe”: 已加载“C:\Users\Administrator\Desktop\C++ project\Debug\C++ project.exe”,已加载符号。
“C++ project.exe”: 已加载“C:\Windows\System32\ntdll.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\kernel32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\KernelBase.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Program Files\AVAST Software\Avast\snxhk.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\msvcp100d.dll”,已加载符号。
“C++ project.exe”: 已加载“C:\Windows\System32\msvcr100d.dll”,已加载符号。
“C++ project.exe”: 已加载“C:\Windows\System32\user32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\gdi32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\lpk.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\usp10.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\msvcrt.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\imm32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\msctf.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\advapi32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\sechost.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\rpcrt4.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\uxtheme.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Program Files\360\360Safe\safemon\safemon.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\shell32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\shlwapi.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\ole32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\oleaut32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\msvcp60.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\wininet.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\urlmon.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\crypt32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\msasn1.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\iertutil.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\psapi.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\ws2_32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\nsi.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\version.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\dwmapi.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7600.16661_none_420fe3fa2b8113bd\comctl32.dll”,Cannot find or open the PDB file
“C++ project.exe”: 已加载“C:\Windows\System32\cryptbase.dll”,Cannot find or open the PDB file
C++ project.exe 已触发了一个断点
HEAP[C++ project.exe]: Invalid address specified to RtlValidateHeap( 00100000, 001038D0 )
Windows 已在 C++ project.exe 中触发一个断点。

其原因可能是堆被损坏,这说明 C++ project.exe 中或它所加载的任何 DLL 中有 Bug。

原因也可能是用户在 C++ project.exe 具有焦点时按下了 F12。

输出窗口可能提供了更多诊断信息。
程序“[3652] C++ project.exe: 本机”已退出,返回值为 3 (0x3)。
kitty_ling0921 2012-05-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

检查下是不是指针或者数组的越界访问
[/Quote]

检查不出有啥问题,而且这个程序不是很复杂,指针调用应该很清楚的,实在搞不懂为什么报错
SONG_CA 2012-05-06
  • 打赏
  • 举报
回复
感觉没错误,把你的工程rebuild all一下
pengfoo 2012-05-06
  • 打赏
  • 举报
回复
检查下是不是指针或者数组的越界访问

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧