注册 登录
编程论坛 C++教室

求字符串长度 总是说 ch 没有定义,我定义了结果错得更多了,该怎么办? 帮看看吧!

晓小晓小 发布于 2014-06-10 22:42, 487 次点击
#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;

}  
3 回复
#2
funyh2502014-06-11 00:51
如果show()是strtype里的话
翻翻书 查查类中的成员函数在类外面是如何定义的
#3
律吕调阳2014-06-11 12:12
#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;
}
#4
yangfrancis2014-06-11 15:39
要定义show的话在它前面加个strtype::
1