| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 487 人关注过本帖
标题:求字符串长度 总是说 ch 没有定义,我定义了结果错得更多了,该怎么办? 帮 ...
只看楼主 加入收藏
晓小晓小
Rank: 2
等 级:论坛游民
帖 子:12
专家分:38
注 册:2014-5-15
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:3 
求字符串长度 总是说 ch 没有定义,我定义了结果错得更多了,该怎么办? 帮看看吧!
#include<iostream>
#include<string>
using namespace std;
class strtype
{
public:
    string  ch;
    int show();


};
int show()
{
    int len=0;
while(ch!='\0')
{
    len++;
}
cout<<"字符串的长度"<<len<<endl;
return 0;
}
int main()
{
strtype *p;
p=new strtype;
cout<<"Creating string"<<endl;
p->ch="we study new and delete";
cout<<"Deleting string"<<endl;
return 0;

}  
搜索更多相关主题的帖子: public 字符串 include 
2014-06-10 22:42
funyh250
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:26
帖 子:290
专家分:1573
注 册:2013-12-25
收藏
得分:6 
如果show()是strtype里的话
翻翻书 查查类中的成员函数在类外面是如何定义的

学习是大事   吃喝拉撒睡是小事   其他的那都不是事
2014-06-11 00:51
律吕调阳
Rank: 2
等 级:论坛游民
威 望:2
帖 子:16
专家分:93
注 册:2014-6-9
收藏
得分:8 
#include<iostream>
#include<string>
using namespace std;
class strtype
{
public:
    string  ch;
    int show();
    const strtype& operator =(const string &str)
    {
        ch = str;
        return *this;
    }
};
int strtype::show()
{
    int len = 0;
    int i = 0;
    while (ch[i++] !=  '\0')
    {
        len++;
    }
    cout << "字符串的长度" << len << endl;
    return 0;
}
int main()
{
    strtype *p;
    p = new strtype;
    cout << "Creating string" << endl;
    p->ch = "we study new and delete";
    cout << "Deleting string" << endl;
    p->show();
    cout << p->ch << endl;
    system("pause");
    return 0;
}
2014-06-11 12:12
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:6 
要定义show的话在它前面加个strtype::
2014-06-11 15:39
快速回复:求字符串长度 总是说 ch 没有定义,我定义了结果错得更多了,该怎么办 ...
数据加载中...
 
   



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

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