| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2454 人关注过本帖
标题:[求助]这个strcmp()函数哪错了
取消只看楼主 加入收藏
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
 问题点数:0 回复次数:6 
[求助]这个strcmp()函数哪错了

如下小程序:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> v;
string s;
char* p="!";
while(cin>>s)
{
v.push_back(s);
if(strcmp(s,p)==0)//编译时这句有错,error C2664: 'strcmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
break;
}
return 0;
}

搜索更多相关主题的帖子: strcmp 函数 std char string 
2006-03-26 15:45
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> v;
string s;
char* p="!";
while(cin>>s)
{
v.push_back(s);
if(strcmp(s,*p)==0)//改过来也是不行的,
break;
}
return 0;
}
或者这样都不可以的
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> v;
string s;
while(cin>>s)
{
v.push_back(s);
if(strcmp(s,"!")==0)
break;
}
return 0;
}

woyaochengshuyidianle 我真的什么也不会
2006-03-26 16:54
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 

上面两个都产生了这样的错误:
\新建文件夹\Cpp11.cpp(12) : error C2664: 'strcmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

Cpp11.obj - 1 error(s), 0 warning(s)


woyaochengshuyidianle 我真的什么也不会
2006-03-26 16:55
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 
To:师妃暄,你帮我调试一下这个程序,不用修改,只要告诉我哪个地方错了,就可以,谢谢你
#include <string>
#include <iostream>
using namespace std;
int main()
{
char*p1="as";
char s[9];
cin>>s;
char* p2=s;
while(strcmp(p1,p2)>0)
strcpy(p1,p2);
cout<<p1<<endl;
return 0;
}

woyaochengshuyidianle 我真的什么也不会
2006-03-26 17:20
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 

我说怎么上不了,把用户名输到密码里去了,真笨啊


woyaochengshuyidianle 我真的什么也不会
2006-03-27 17:55
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 
以下是引用名人在2006-3-27 21:20:00的发言:

定义一个指针类型的变量跟定义一个普通变量表面看来是一样的,但绝对是两个概念。指针类型的变量在定义的时候如果没有初始化,默认情况下它会随意指向一个地址,而它却是个无效的地址,必须对它进行初始化或者说分配一块内存。而普通变量在定义的时候如果没有初始化某些编译器会默认把它初始化为零。

名人,在帮我一下

#include <string>
#include <iostream>
using namespace std;
int main()
{
char*p1="as";//我在这边已经给p1初始化了,也就是给他分配了内存空间了
char s[9];
cin>>s;
char* p2=s;
while(strcmp(p1,p2)>0)//为什么在这边还要再给他分配空间呢?p1 = new char[10];
strcpy(p1,p2);
cout<<p1<<endl;
return 0;
}


woyaochengshuyidianle 我真的什么也不会
2006-03-27 21:40
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 

再说一次谢谢你


woyaochengshuyidianle 我真的什么也不会
2006-03-28 14:13
快速回复:[求助]这个strcmp()函数哪错了
数据加载中...
 
   



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

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