[英]error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’)
[英]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
我是學習數據結構和算法的初學者。 我正在嘗試這個:
#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;
}
但編譯器顯示以下錯誤:
prog.cpp:26:14: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’)
cout << library.pop() ;
我使用cout <<
很多次,但從未遇到過這個錯誤。
與您的直覺相反, std::stack::pop()
不返回任何內容( void
)。 https://en.cppreference.com/w/cpp/container/stack/pop和void
無法打印。
你可能想要這個:
for(int j=0; j < library.size(); j++){
cout << library.top() ;
library.pop();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.