[英]How to fix error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void') while using strings and stack
I am a beginer learning Data Structures and Algorithms.我是学习数据结构和算法的初学者。 I was trying this:
我正在尝试这个:
#include<iostream>
#include<ostream>
#include<stack>
#include<string>
using namespace std;
int main (){
string original ;
string a = "";
std::stack<string> library;
cin >> original;
for(int i=1; i < original.size() -1; i++){
char b = original[i];
if(!((b == '/' ) || (b == '\\' ))){
a = a + b;
}
else{
library.push(a);
a = "";
};
};
for(int j=0; j < library.size(); j++){
cout << library.pop() ;
}
return 0;
}
but the compiler is showing the following error:但编译器显示以下错误:
prog.cpp:26:14: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’)
cout << library.pop() ;
I have used cout <<
many times, but never faced this error.我使用
cout <<
很多次,但从未遇到过这个错误。
Contrary to your intuition, std::stack::pop()
doesn't return anything ( void
).与您的直觉相反,
std::stack::pop()
不返回任何内容( void
)。 https://en.cppreference.com/w/cpp/container/stack/pop and void
cannot be printed. https://en.cppreference.com/w/cpp/container/stack/pop和
void
无法打印。
You probably want this:你可能想要这个:
for(int j=0; j < library.size(); j++){
cout << library.top() ;
library.pop();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.