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

'string' : undeclared identifier,这是怎么回事?

heyyroup 发布于 2007-08-13 18:42, 11183 次点击

/*编写一个函数,它接受一个指向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'
我感觉我在使用之前有定义啊,到底是哪里出了问题呢?



7 回复
#2
maoguoqing2007-08-13 19:01
#include &lt;cstring&gt;
#3
heyyroup2007-08-13 19:14
我试过呢,还是一样的问题啊
#4
maoguoqing2007-08-13 19:32
再加个using namespace std;看看
#5
terisevend2007-08-13 20:15
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编辑过]

#6
heyyroup2007-08-13 20:53
以下是引用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]);
}
}

#7
heyyroup2007-08-13 21:04
我知道怎么回事了,把cin&gt;&gt;str1;换成面对行的输入就可以了,谢谢大家的帮助
#8
terisevend2007-08-13 21:32
忘记告诉LZ了...using namespace std; 如果在函数,类,结构体等等的外面的话...后面的函数体内不用再用using namespace std;
1