简体   繁体   English

如何修复错误:'operator&lt;&lt;' 不匹配(操作数类型为 'std::ostream {aka std::basic_ostream<char> }' 和 'void')同时使用字符串和堆栈</char>

[英]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/popvoid无法打印。

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.

相关问题 错误:“operator&lt;&lt;”不匹配(操作数类型为“std::ostream {aka std::basic_ostream”<char> }&#39; 和 &#39;void&#39;) - error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’) 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '}</char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream {aka std::basic_ostream<char> }'和'列表')</char> - error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'List') 错误:'operator<<' 不匹配(操作数类型为'std::ostream {aka std::basic_ostream<char> }' 和 'std::ostream {aka std::basic_ostream<char> }')</char></char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::ostream {aka std::basic_ostream<char>}’) 'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream {aka std::basic_ostream<char> }' 和 '分数')</char> - No match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Fraction') 与&#39;operator &lt;&lt;&#39;不匹配(操作数类型为&#39;std :: ostream {aka std :: basic_ostream <char> }” - no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'std::_List_iterator<int> ')</int></char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::_List_iterator<int>’) 错误:“operator&lt;&lt;”不匹配(操作数类型为“std::basic_ostream”<char> &#39; - error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ \\ main.cpp | 103 |错误:“ operator &lt;&lt;”不匹配(操作数类型为“ std :: ostream {aka std :: basic_ostream <char> }”和“人”) - \main.cpp|103|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Person') 运算符重载时出错(错误:“运算符&lt;&lt;”不匹配(操作数类型为“std::basic_ostream”)<char> &#39; 和 &#39;const 字符 [2]&#39;) - Error while operator overloading (error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const char [2]’)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM