【问题标题】:no match for operator '<<' Can someone aid with void function error? [c++]no match for operator '<<' 有人可以帮助解决 void 函数错误吗? [c++]
【发布时间】:2016-03-10 03:36:34
【问题描述】:

This post 处理相同的错误,但发帖人在使用 void 函数时没有问题。

This 帖子涉及“void”类型的函数,但建议发帖者将函数类型更改为“string”,这对我的情况没有帮助。

我的代码执行良好,除了输出末尾的文字“0”。当我将函数类型更改为“void”时,遇到了上述错误。

我已经通过这个教程无数次,并且已经彻底搜索,但仍然无法解决这个问题。

//my code
#include <iostream>
using namespace std;

int intervalcountdown (int a, int b) {
    for(a; a>0; a = a - b) {

        cout << a;

        if(a<=b) {

        break;
        }

        cout << ",";


    }
    cout << ".";
    return 0;
}


int main () {
    cout << intervalcountdown(20,3);
    return 0;
}

【问题讨论】:

    标签: c++ function return-value void


    【解决方案1】:

    只是不要打印你不想要的东西。

    #include <iostream>
    using namespace std;
    
    void intervalcountdown (int a, int b) { // change return type to void
        for(; a>0; a = a - b) { // meaningless a is removed
    
            cout << a;
    
            if(a<=b) {
    
                break;
            }
    
            cout << ",";
    
    
        }
        cout << ".";
        // remove the return statement because the return type is now void
    }
    
    
    int main () {
        intervalcountdown(20,3); // remove extra printing
        return 0;
    }
    

    【讨论】:

    • 这很简单。在将类型更改为“void”并删除“return 0;”后,我未能删除额外的打印。出于某种原因,我的印象是在 main () 函数的某个地方仍然需要“cout
    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 2023-03-28
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多