首页 新闻 赞助 找找看

为什么i和j会是未声明的标识符?

0
[待解决问题]

void Editstring::add(Tstring &s)
{
int n,k;
char *pt;
pt=s.getstr();
n=s.getlen();
char *newstr=new char[n+len+1];
for(int i=0;i<cursor;i++)
newstr[i]=str[i];
k=i;
for(int j=0;j<n;i++,j++)
newstr[i]=pt[j];
cursor=i;
for(j=k;j<=len;j++,i++)
newstr[i]=str[j];
delete []str;
str=new char[n+len+1];
strcpy(str,newstr);
delete []newstr;
}

山顶水下的主页 山顶水下 | 菜鸟二级 | 园豆:202
提问于:2017-03-09 21:49
< >
分享
所有回答(3)
0
void Editstring::add(Tstring &s)
{
int n,k,i,j;
char *pt;
pt=s.getstr();
n=s.getlen();
char *newstr=new char[n+len+1];
for(i=0;i<cursor;i++)
newstr[i]=str[i];
k=i;
for(j=0;j<n;i++,j++)
newstr[i]=pt[j];
cursor=i;
for(j=k;j<=len;j++,i++)
newstr[i]=str[j];
delete []str;
str=new char[n+len+1];
strcpy(str,newstr);
delete []newstr;
}
//你的 i和 j 都是在循环里面声明的,属于局部变量。作用域在循环里面。出了循环就没效果了。
BUTTERAPPLE | 园豆:3190 (老鸟四级) | 2017-03-09 22:20

你把 i 和 j 在开头就定义好。

支持(2) 反对(0) BUTTERAPPLE | 园豆:3190 (老鸟四级) | 2017-03-09 22:21

声明后运行,在strcpy(str,newstr)卡住了,运行不下去

支持(0) 反对(0) 山顶水下 | 园豆:202 (菜鸟二级) | 2017-03-10 11:02

@山顶水下: 你先看看你写的代码,有 for 语句的都用大括号 { } 包含起来,要不然你每个语句的作用域自己看的都不清楚。

支持(0) 反对(0) BUTTERAPPLE | 园豆:3190 (老鸟四级) | 2017-03-10 11:14

@BUTTERAPPLE: 

//estring.h #include #include class Tstring { protected: int len; char *str; public: Tstring(char *); ~Tstring(); void setstr(char *s); int getlen(); char *getstr(); void disp(); }; class Editstring:public Tstring { private: int cursor; public: Editstring(char *s,int); ~Editstring(); void movecursor(int off); void add(Tstring&s); void replace(Tstring&s); void erase(int pos); int getcursor(){return cursor;} }; //estring.cpp #include #include"estring.h" //#include //#include Tstring::Tstring(char *s=NULL) { len=strlen(s); str=new char[len+1]; strcpy(str,s); } Tstring::~Tstring() { delete []str; } void Tstring::setstr(char *s) { if(strlen(s)!=len) { delete []str; len=strlen(s)+1; str=new char[len+1]; } strcpy(str,s); } int Tstring::getlen() { return len; } char * Tstring::getstr() { return str; } void Tstring::disp() { std::cout<<str<<std::endl; } Editstring::Editstring(char *s,int off):Tstring(s) { cursor=off; } Editstring::~Editstring(){} void Editstring::movecursor(int off) { cursor=off>len?len:off; } void Editstring::add(Tstring &s) { int n,k,i,j; char *pt; pt=s.getstr(); n=s.getlen(); char *newstr=new char[n+len+1]; for(int i=0;i<cursor;i++) newstr[i]=str[i]; k=i; for(int j=0;j<n;i++,j++) newstr[i]=pt[j]; cursor=i; for(j=k;j<=len;j++,i++) newstr[i]=str[j]; delete []str; str=new char[n+len+1]; strcpy(str,newstr); delete []newstr; } void Editstring::replace(Tstring &s) { int n,t; char *pt; int i; pt=s.getstr(); n=s.getlen(); t=n+cursor<=len?n+cursor:len; for(int i=cursor,j=0;i<t;j++,i++) str[i]=pt[j]; cursor=i; } void Editstring::erase(int pos) { if(cursor+pos<len) { for(int i=cursor;i<=len;i++) str[i]=str[i+pos]; } else str[cursor]='\0'; } //main.cpp # include"estring.h" using namespace std; void main() { Tstring s1("visual"); Tstring s2("???"); Editstring es("Object-oriented programming!",0); std::cout<<"编辑字符串:"; es.disp(); std::cout<<"光标当前位置:"<<es.getcursor()<<std::endl; es.movecursor(16); std::cout<<"移动后光标位置:"<<es.getcursor()<<std::endl; std::cout<<"要插入的字符串:"; s1.disp(); es.add(s1); std::cout<<"插入后字符串:"; es.disp(); std::cout<<"当前光标位置:"<<es.getcursor()<<std::endl; es.movecursor(7); std::cout<<"移动后的光标位置:"<<es.getcursor()<<std::endl; std::cout<<"替换字符串:"; s2.disp(); es.replace(s2); std::cout<<"替换后字符串:"; es.disp(); es.erase(5); std::cout<<"删除后字符串:"; es.disp(); }

支持(0) 反对(0) 山顶水下 | 园豆:202 (菜鸟二级) | 2017-03-10 16:35

@BUTTERAPPLE: 

支持(1) 反对(0) 山顶水下 | 园豆:202 (菜鸟二级) | 2017-03-10 16:38
1

加大括号..写代码好好加大括号.

吴瑞祥 | 园豆:29449 (高人七级) | 2017-03-10 08:52
0

for循环,不要括号吗????
for (int i = 0; i < 10; i++) {

}
不在定义的范围内

四月的天 | 园豆:209 (菜鸟二级) | 2017-03-13 10:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册