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

简单问题 哪里错了....我菜鸟

momo1212825 发布于 2008-04-24 07:32, 1019 次点击
#include<iostream>
#include<cstring>
int main()
{
    using namespace std;
    cout<<"Enter a word:";
    string word;
    cin>>word;
    char temp;
    int i,j;
    for(j=0,i=word.size()-1;j<i;--i,++j)
    {
        temp=word[i];
        word[i]=word[j];
        word[j]=temp;
    }
    cout<<word<<"\nDone\n";
    return 0;
}

E:\Program Files\Microsoft Visual Studio\MyProjects\1111\111.cpp(8) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (
or there is no acceptable conversion)
E:\Program Files\Microsoft Visual Studio\MyProjects\1111\111.cpp(17) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
(or there is no acceptable conversion)
6 回复
#2
heyyroup2008-04-24 09:07
这已经很明显了,string 类里面没有重载"<<",">>"操作符,你用的是VC6.0吗,查查MSDN就可以看到string 类的详细内容了。
#3
momo12128252008-04-24 09:14
恩 我用的6.0  MSDN怎么查啊...请指教,,,,
#4
crosoli2008-04-24 13:02
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string word;
    cout<<"Enter a word:";
    cin>>word;
    char temp;
    int i,j;
    for(j=0,i=word.size()-1;j<i;--i,++j)
    {
        temp=word[i];
        word[i]=word[j];
        word[j]=temp;
    }
    cout<<word<<"\nDone\n"<<endl;
    return 0;
}


至多一个C,注意格式,养成习惯
#5
忘记喧嚣2008-04-24 13:07
把#include<cstring> 改成:
#include<string>

OK!

还有就是最好
#include<iostream>
#include<string>
using namespace std;
int main()
{
    //using namespace std;
    cout<<"Enter a word:";
    string word;
    cin>>word;
    char temp;
    int i,j;
    for(j=0,i=word.size()-1; j<i; --i,++j)
    {
        temp=word[i];
        word[i]=word[j];
        word[j]=temp;
    }
    cout<<word<<"\nDone\n";
    return 0;
}
这样写
#6
momo12128252008-04-24 21:35
谢谢咯
#7
wsz317sky2008-04-27 16:26
<cstring>
定义的一些字符串的函数,比如strcpy(),strlen()等等。
<string>
定义了string类
1