取子串的问题:String Substr(int index,int count),但是其中的2个错误不会改,大家帮帮忙!
取子串:
#include <iostream>
#include <string>
using namespace std;
class String
{
private:
int stringlen;
char *s;
public:
String Substr(int index,int count);
};
String String ::Substr(int index,int count)
{
//从i到串尾的字符个数
int leftlen=stringlen-index-1;
int i,tlen;
//建立子串
string t;
char *p,*q;
//若i 越界,返回空串
//if(i>leftlen)
// return t;
//若n大于剩下的字符,则只用剩下的字符
if(count>=leftlen)
count=leftlen;
//删除定义t时产生的null串
delete[] t;
t=new char[count+1];
if (t==NULL)
{cout<<"Error"<<endl;
//exit(1);
}
for(i=0,p=t,q=&s[index];i<count;i++)
*p++=*q++;
*p=0;
tlen=count+1;
return t;
}
int main()
{
String string1("ABCED");
String string2=string1.Substr(2,4);
//cout<<string2<<endl;
return 0;
}
这是编译时 其中的错误:
f:\my project\my docment\my doucment.cpp(37) : error C2440: 'delete' : cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to ''
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
f:\my project\my docment\my doucment.cpp(37) : fatal error C1903: unable to recover from previous error(s); stopping compilation
执行 cl.exe 时出错.