内存不能读,请指教,谢谢!

puppet_love 2006-10-03 02:21:38
我在编写以下程序时,编译没有错误,执行时说:
"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;
}
...全文
215 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
luolovegui 2006-10-03
  • 打赏
  • 举报
回复
晕,不好意思,

太长了.. 累...


------------------------------------------------------------------------
快两年没有回答问题了,2006年10月2号重新回答问题,开始搛星星.

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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