请指正错误,谢谢!

puppet_love 2006-10-03 01:29:12
我在编写以下程序时,编译没有错误,执行时说:
"0x00402050"指令引用的"0xccdfc7c4"内存.该内存不能为"read".
为什么不能都,高手指正,谢谢!

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Sequence{
public:
bool adds(int ,const string &);
bool del(int);
void output()const;
Sequence():last(-1){}
Sequence(const char *);
~Sequence();
protected:
enum {MaxStr =50};
string s[MaxStr];
int last;
private:
string filename;
ifstream in;
ofstream out;
};
bool Sequence::adds(int pos,const string & entry){
//last+1 是紧跟着最后一个元素在加入;
//如果pos>last+1说明在last+1处没有加入元素,跳过一个位置去添加显然在序列里面是错误的;
if(last==MaxStr-1||pos<0||pos>last+1)
return false;
for(int i=last;i>=pos;i--)
s[i+1]=s[i];
s[pos]=entry;
last++;
return true;
}
bool Sequence::del(int pos)
{
if (pos <0||pos > last)
return false;
for(int i=pos ;i <last;i++)
s[i]=s[i+1];
last--;
return true;
}
void Sequence::output()const{
for(int i;i <=last;i++)
cout<<i<<" "<<s[i]<<endl;
}
Sequence::Sequence(const char * fname){
last=-1;
filename=fname;
in.open(fname);
if(!in)
return;
while(last<MaxStr-1&&getline(in,s[last+1]))
last++;
in.close();
}
Sequence::~Sequence(){
if(filename=="")
return;
out.open(filename.c_str());
for(int i=0;i<=last;i++)
out<<s[i]<<endl;
out.close();
}
class SortedSeq:public Sequence{
public:
bool addss(const string&);
SortedSeq(){}
SortedSeq(const char*);
protected:
void sort();
private:
using Sequence::adds;
};
void SortedSeq::sort(){
string temp;
int i,j;
for(i=0;i<=last-1;i++){
temp=s[i];
for(j=i;j>=0;j--)
if(temp<s[i])
s[j+1]=s[j];
else
break;
s[j+1]=temp;//s[i]=temp;
}
}
bool SortedSeq::addss(const string & entry){
int i;
for(i=0;i<=last;i++)
if(entry<=s[i])
break;
return adds(i,entry);
}
SortedSeq::SortedSeq(const char* fname):Sequence(fname){
sort();
}



#include "sequence.h"
int main()
{
string inbuff,where;
int wh;
Sequence items("test.dat");
while(true)
{
cout<<"\nSequence output : \n";
items.output();
cout<<"\n--add\n"
<<"2--delete\n"
<<"3--quit\n";
getline(cin,inbuff);
if( inbuff=="1")
{
cout<<"\nitem to add :";
getline(cin ,inbuff);
cout<<"add where ?";
getline(cin,where);
//atoi ()函数作用是把字符形式的数字转换为数值形式的数字即“1”——》1
wh=atoi(where.c_str());
if(items.adds(wh,inbuff))
cout<<"item added\n";
else
cout<<"item not added\n";
}
else if (inbuff=="2")
{
cout<<"\nwhere to delete : ";
getline(cin ,where);
wh=atoi(where.c_str());
if(items.del(wh))
cout<<"item deleted \n";
else
cout<<"item not deleted\n";
}
else if (inbuff=="3")
break;
}
return 0;
}
...全文
421 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
puppet_love 2006-10-04
  • 打赏
  • 举报
回复
谢谢,刚学VC,对环境不是太熟悉!
jixingzhong 2006-10-04
  • 打赏
  • 举报
回复
这个错误的原因就是访问了非法内存,
楼主你完全可以通过断点单步运行找到问题语句,
然后进行验证的 ...
puppet_love 2006-10-04
  • 打赏
  • 举报
回复
xie xie
OOPhaisky 2006-10-03
  • 打赏
  • 举报
回复
void Sequence::output()const{
for(int i = 0;i <=last;i++)//此处如果只写“int i;”,则i的初始值是随机的(而不是0),因为i是局部变量,而非全局变量,编译器不会自动将i清0,这样一来,s[i]就可能引用非法内存了
cout<<i<<" "<<s[i]<<endl;
}
shinjikun 2006-10-03
  • 打赏
  • 举报
回复
另外,SortedSeq的话,干吗不插排啊?
shinjikun 2006-10-03
  • 打赏
  • 举报
回复
对,哪个地方居然没写int i=0
Dugowe 2006-10-03
  • 打赏
  • 举报
回复
程序太长了..
sinall 2006-10-03
  • 打赏
  • 举报
回复
void Sequence::output()const{
for(int i=0;i <=last;i++)
cout<<i<<" "<<s[i]<<endl;
}

64,677

社区成员

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

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