| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 11164 人关注过本帖
标题:'string' : undeclared identifier,这是怎么回事?
只看楼主 加入收藏
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
 问题点数:0 回复次数:7 
'string' : undeclared identifier,这是怎么回事?

/*编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转化成大写,为此可以
使用表6.4描述的函数toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数
该程序的运行如下:
Enter a string (q to quit):go away
GO AWAY
Enter a string (q to quit):good grief!
GOOD GRIEF!
Enter a string (q to quit):q
Bye.
*/
#include <iostream>
#include <string>
#include <cctype>
void convert(string &str);
int main()
{
using namespace std;
string str1;
cout<<"Enter a string(q to quit):\n";
cin>>str1;
if (str1!='q')
{
while (str1)
{
convert(str1);
cout<<str1;
cout<<"Enter a string(q to quit):\n";
cin>>str1;
}
}
else
cout<<"Bye.\n";
return 0;
}
void convert(string &str)
{
using namespace std;
int limit=strlen(str);
for(int i=0;i<limit;i++)
{
if (isalpha(str[i]))
toupper(str[i]);
}
}

编译的时候总是说:
error C2065: 'string' : undeclared identifier
error C2065: 'str' : undeclared identifier
error C2182: 'convert' : illegal use of type 'void'
我感觉我在使用之前有定义啊,到底是哪里出了问题呢?



搜索更多相关主题的帖子: identifier string undeclared 
2007-08-13 18:42
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 
#include &lt;cstring&gt;

天行健,君子以自强不息!!QQ:68660681
2007-08-13 19:01
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
得分:0 
我试过呢,还是一样的问题啊
2007-08-13 19:14
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 
再加个using namespace std;看看

天行健,君子以自强不息!!QQ:68660681
2007-08-13 19:32
terisevend
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2007-6-2
收藏
得分:0 
if (str1!='q')
{
while (str1)
{
convert(str1);
cout<<str1;
cout<<"Enter a string(q to quit):\n";
cin>>str1;
}


改成

while( str1 != "q" && str1 != "Q" )
if( str1.length( ) )
{
convert( str1 );
cout << str1 << endl;
cout << "ENTER A STRING( Q TO QUIT ). " << endl;
getline( cin, str1 ); //修正后, 可读入空白字符
}

还有```
strlen(str); 改成 str.length();

至于结果呢```调试完毕...根本无法输出LZ所需要的结果...也就是小写的还是小写, 大写的还是大写...

如果不清楚函数的使用,可以自己编写大小写转换函数```

PS:

void convert( string& str_t )
{
int limit = str_t.length();

for( int i = 0; i < limit; i++ )
if( str_t[i] >='A' && str_t[i] <= 'Z' )
str_t[i] += 32;
}

[此贴子已经被作者于2007-8-13 21:42:38编辑过]


2007-08-13 20:15
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
得分:0 
以下是引用terisevend在2007-8-13 20:15:12的发言:
if (str1!='q')
{
while (str1)
{
convert(str1);
cout<<str1;
cout<<"Enter a string(q to quit):\n";
cin>>str1;
}


改成

while( str1 != "q" && str1 != "Q" )
if( str1.length( ) )
{
convert( str1 );
cout << str1 << endl;
cout << "ENTER A STRING( Q TO QUIT ). " << endl;
cin >> str1;
}

还有```
strlen(str); 改成 str.length();

至于结果呢```调试完毕...根本无法输出LZ所需要的结果...也就是小写的还是小写, 大写的还是大写...

如果不清楚函数的使用,可以自己编写大小写转换函数```

PS:

void convert( string& str_t )
{
int limit = str_t.length();

for( int i = 0; i < limit; i++ )
if( str_t[i] >='A' && str_t[i] <= 'Z' )
str_t[i] += 32;
}
我把程序改了一下,能实现大小写的转换,但是又出现一个问题,我的字符串里面不能出现空格。。。

/*编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转化成大写,为此可以
使用表6.4描述的函数toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数
该程序的运行如下:
Enter a string (q to quit):go away
GO AWAY
Enter a string (q to quit):good grief!
GOOD GRIEF!
Enter a string (q to quit):q
Bye.
*/
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void convert(string &str);
int main()
{
using namespace std;
string str1;
cout<<"Enter a string(q to quit):\n";
cin>>str1;
while(str1!="q"&&str1!="Q")
{
if (str1.length())
{
convert(str1);
cout<<str1<<endl;
cout<<"Enter a string(q to quit)\n";
cin>>str1;
}
}
return 0;
}
void convert(string &str)
{
using namespace std;
int limit=str.length();
for(int i=0;i<limit;i++)
{
if (isalpha(str[i]))
str[i]=toupper(str[i]);
}
}

2007-08-13 20:53
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
得分:0 
我知道怎么回事了,把cin&gt;&gt;str1;换成面对行的输入就可以了,谢谢大家的帮助
2007-08-13 21:04
terisevend
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2007-6-2
收藏
得分:0 
忘记告诉LZ了...using namespace std; 如果在函数,类,结构体等等的外面的话...后面的函数体内不用再用using namespace std;

2007-08-13 21:32
快速回复:'string' : undeclared identifier,这是怎么回事?
数据加载中...
 
   



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

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