大家帮忙看下这个程序怎么会报错?
admin 发表于 2010-04-21 | 来源:互联网 | 阅读:

#include<iostream>
#include<stack>
#include<queue>

using namespace std;

int main()
{
stack<string> strstack;
string str;

cout<<"input some words"<<endl;
while(cin>>str) //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
{
strstack.push(str);
}

  if (strstack.empty()==true)
  cout<<"you have not input any words"<<endl;
else while (str.empty()==true)
{
cout<<"the words are"<<str.top()<<endl;
str.pop();
}
 
}

已经有9 个评论
  1. wuwei04 说:

    #include <string>

  2. waterskin 说:

    str.pop();  str.top();这两个方法并不是string 库中所包含的,即便是如楼上的#include <string>,也还是会报错的

  3. nashliu 说:

    按照你的意图给你修改了一下:C/C++ code
    #include <iostream>
    #include <stack>
    #include <queue>
    int main()
    {
    stack <string> strstack;
    string str;

    cout <<"input some words" <<endl;
    while(cin>>str) //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
    {
    strstack.push(str);
    }

    if (strstack.empty()==true)
    cout <<"you have not input any words" <<endl;
    else while (str.empty()==true)
    {
    cout <<"the words are" <<strstack.top() <<endl; //11111111
    strstack.pop(); //222222222
    }

    }

  4. boysee 说:

    另外,揣测你的意图是想在else while (str.empty()==true) 这个条件下输出栈内元素。那么请把这个改为else while (str.empty() != true)  或者else while (!str.empty()) 

  5. 96789123 说:

    string 可没有top和pop成员函数

  6. wtaxum 说:

    还有一点把while循环改一下吧while(true)  {  cin>>strif(str == "exit") break;strstack.push(str);  }  因为cin只是一个对象,不存在返回值。。。。

  7. 风子 说:

    str.pop()和str.empty()似乎应该是strstack.pop()和strstack.empty()

  8. mikeyao210 说:

    C/C++ code
    int main()
    {
    stack <string> strstack;
    string str;

    cout <<"input some words" <<endl;
    while(cin>>str)
    {
    strstack.push(str);
    }

    if (strstack.empty()==true)
    cout <<"you have not input any words" <<endl;
    else// while (str.empty()==true) 全是粗心大意 把strstack写成str
    while(strstack.empty() != true)
    {
    //cout < <"the words are" < <str.top() < <endl; 同上
    cout<<"the words are "<<strstack.top()<<endl;
    //str.pop(); 同上
    strstack.pop();
    }

    }

  9. aw3fae 说:

    string也有pop。。。?我想楼主这个问题无解。楼主会把MSDN给玩死。

我要评论

评论功能因故关闭!

请加入我们的QQ群一起参与讨论:群号59400482(500人超级群)


返回首页 | 关于我们 | 联系我们 | 广告合作 | 网站地图 | 友情链接 | 版权声明