c++ - 在for循环C++中调用Void函数

标签 c++ function loops error-handling

这是我在StackOverflow上的第一篇文章!我的Intro C++类项目遇到问题。我在for循环中的调用中收到E0349错误,“没有运算符匹配这些操作数”。我在这里看过类似的问题,但没有找到解决方案。如果我格式化错误或不了解很多,我深表歉意。这是我上编程的第一个类。
我的代码:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void disp2xInt(int num);

int main() {
    // Do "Press any key to continue..." on exit
    atexit([] {system("pause"); });

    // Print out your name and course
    cout << "Jacob" << endl;
    cout << "ELET115N" << endl << endl;

    for (int i = 0; i < 5; i++) { 
        cout << disp2xInt(i) << "\n";
        cout << "Hello world!!!\n"; 
    }
    disp2xInt(9);

    return 0;
}

void disp2xInt(int n) {
    cout << "2 x " << n;
    cout << " = " << n * 2; 
    cout << endl;
}

最佳答案

您不能cout返回无效的方法。此外,在这种情况下没有必要,因为该方法本身已经调用了cout。做就是了 :

int main() {
    // Do "Press any key to continue..." on exit
    atexit([] {system("pause"); });

    // Print out your name and course
    cout << "Jacob" << endl;
    cout << "ELET115N" << endl << endl;

    for (int i = 0; i < 5; i++) { 
        disp2xInt(i);
        cout << "Hello world!!!\n"; 
    }
    disp2xInt(9);

    return 0;
}

关于c++ - 在for循环C++中调用Void函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64755627/

相关文章:

loops - 在 Logramm 中循环字典

java - 有效地找到匹配的对象对

javascript - 是否可以调用使用不同参数第二次自动执行的 x.Enter?

javascript - 如何推断变量使它们成为全局变量?

r - 为随机抽样构建 R 循环

c# - 在两个碰撞器接触的每一帧上运行代码

c++ - 如何使用 FFMPEG 将 N 个实时 MP3 流合并为一个?

c++ - 错误 : no match for 'operator==' (operand types are 'Seat' and 'std::string {aka std::basic_string<char>}' )

c++ - OnPaint MFC 上不显示矩形

C:可以将变量传递给请求结构的函数,以便在调用函数时将这些变量包装到结构中吗?