第一次与 Void 合作

working with void for the first time

本文关键字:合作 Void 第一次      更新时间:2023-10-16

我最近开始弄乱 void 并遇到了一个问题

这是我的代码:

#include <iostream>
using namespace std;
void smallSort();
int main()
{
    int num1, num2, num3;
    cout << "Please enter the first number" << endl;
    cin >> num1;
    cout << "Please enter the second number" << endl;
    cin >> num2;
    cout << "Please enter the third number" << endl;
    cin >> num3;
    cout << "Now I will sort from smallest to biggest!" << endl;
    smallSort();
    return 0;
}
void smallSort(int& num1, int& num2, int& num3){
    if(num1 > num2)
        swap(num1, num2);
    if(num1 > num3)
        swap(num1, num3);
    if(num2 > num3)
        swap(num2, num3);
    cout << num1 << " " << num2 << " " << num3 << endl;
}

我试图将参数添加到主节点中的 smallSort 中,但它说参数太多了。 我也尝试从空白中删除参数,但这也不起作用。 任何提示或任何我能阅读的东西都会很棒,谢谢

您的函数定义与其声明不匹配:

void smallSort(); // <== zero args
void smallSort(int& num1, int& num2, int& num3){ // <== three args

这些必须精确匹配。您的声明应更改为:

void smallSort(int&, int&, int&);

此外,您实际上并没有使用任何参数调用smallSort

smallSort();

应该是:

smallSort(num1, num2, num3);