大家帮忙看下这个程序怎么会报错?
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();
}
}

#include <string>
str.pop(); str.top();这两个方法并不是string 库中所包含的,即便是如楼上的#include <string>,也还是会报错的
按照你的意图给你修改了一下: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
}
}
另外,揣测你的意图是想在else while (str.empty()==true) 这个条件下输出栈内元素。那么请把这个改为else while (str.empty() != true) 或者else while (!str.empty())
string 可没有top和pop成员函数
还有一点把while循环改一下吧while(true) { cin>>strif(str == "exit") break;strstack.push(str); } 因为cin只是一个对象,不存在返回值。。。。
str.pop()和str.empty()似乎应该是strstack.pop()和strstack.empty()
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();
}
}
string也有pop。。。?我想楼主这个问题无解。楼主会把MSDN给玩死。