| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1800 人关注过本帖
标题:取子串的问题:String Substr(int index,int count),但是其中的2个错误不会 ...
只看楼主 加入收藏
pjzpjz19880
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-6-6
结帖率:66.67%
收藏
已结贴  问题点数:4 回复次数:2 
取子串的问题: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 时出错.
搜索更多相关主题的帖子: int count String Substr index 
2010-06-16 20:36
debroa723
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:23
帖 子:862
专家分:1954
注 册:2008-10-12
收藏
得分:4 
delete[] t;
string不需要你来管理它的内存。
2010-06-16 21:33
pjzpjz19880
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-6-6
收藏
得分:0 
请你帮我改改吧,我改过很多次了,每次是越改程序运行错的越多,拜托啊!
2010-06-16 21:51
快速回复:取子串的问题:String Substr(int index,int count),但是其中的2个错 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011053 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved