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

【求助】(str = str2)

從霝開匙 发布于 2007-09-11 16:51, 1027 次点击
#include<iostream>
#include<string>
using namespace std;
int main()
{
int errors=0;
string str("a very long literal string");
for(int ix =0;ix<1000000;++ix)
{
int len =str.size();
string str2=str;
if(str != str2)//为什么如果吧条件改成(str=str2)编译就不通过
++errors;
}
cout<<"string class:"
<<errors<<"errors occurred.\n";
return 0;
}
提示出错如下
error C2451: conditional expression of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' is illegal
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
他们不就是一个errors要加,一个errors不加的不同吗?
5 回复
#2
yuyunliuhen2007-09-11 18:20
if(str == str2)

” = “是赋值
#3
cince2007-09-11 18:41
粗心的错误
#4
從霝開匙2007-09-12 12:07
不好意思 ,刚学看了几遍这个说明了新手爱错的
#5
xq07142007-09-12 18:03
if(str==str2)有这样子写的吗?
两个字符串比较的话,比的是地址把,地址肯定是不同的,所以一定不相等把!
应该是strcmp(str,str2)吧!
#6
jiangzw6252007-09-12 20:56
string是标准库里的一个类。
str == str2并不是比较地址,而是调用标准库定义的operator ==方法
1