【问题标题】: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如何修复错误:在使用字符串和堆栈时不匹配 'operator<<'(操作数类型是 'std::ostream {aka std::basic_ostream<char>}' 和 'void')
【发布时间】:2021-08-09 06:12:00
【问题描述】:

我是学习数据结构和算法的初学者。 我正在尝试这个:

#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 &lt;&lt;,但从未遇到过这个错误。

【问题讨论】:

    标签: c++ c++14 cout dsa


    【解决方案1】:

    与您的直觉相反,std::stack::pop() 不会返回任何内容 (void)。 https://en.cppreference.com/w/cpp/container/stack/popvoid 无法打印。

    你可能想要这个:

        for(int j=0; j < library.size(); j++){
            cout << library.top() ;
            library.pop();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      相关资源
      最近更新 更多