简体   繁体   English

如何使用数组和引用参数调用void函数

[英]How to call void function with array and reference arguments

Good evening, I'm attempting to call my void function "getProblems" in main, but get an extraneous value when outputting "getProblems" with no parameters. 晚上好,我试图在main中调用我的void函数“ getProblems”,但是在不带参数的情况下输出“ getProblems”时会得到一个无关的值。 Similarly, when passing arguments, such as "getProblems(list, i)", I get the error "no operator '<<' matches these operands". 类似地,当传递参数时,例如“ getProblems(list,i)”,我收到错误消息“没有运算符'<<'与这些操作数匹配”。 The goal is to output the number of problems my text file contains without using a function that returns a value or using pointers. 目的是在不使用返回值的函数或指针的情况下输出文本文件包含的问题数。

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

using namespace std;
int const MAX_PROBLEMS = 50;
void getProblems(string  problem[], int& count);

int main()
{
string list[MAX_PROBLEMS] = {};
int i = 0;
    cout << "There are " << getProblems << " problems. " << endl;
//  I have also tried calling the void function with parameters
//  cout << "There are " << getProblems(list, i) << "problems. " << endl;

    return 0;
}
void getProblems(string  problem[], int& count)
{

    ifstream mathProblems;
        mathProblems.open("P4Problems.txt");
        if (!mathProblems)
            {
                cout <<"No file was found."<< endl;
            }
        count = 0;
        string data;

        getline(mathProblems, data);
            while (!mathProblems.eof())
                {
                    problem[count] = data;
                    count ++;
                    mathProblems >> data;
                }
        mathProblems.close();
}

Your function getProblems() is of type void , so what are you trying to display with cout ? 您的函数getProblems()的类型为void ,那么您要使用cout显示什么?

If you need to display the count, which is an argument, 如果您需要显示计数(这是一个参数),

int main()
{
    int count;
    getProblems(listt,count); //assuming listt, has been declared before
    cout << "There are " << count << " problems. " << endl;    
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在数组函数指针中调用void - Call void in array function pointer VOID函数通过引用传递数组,但是使用时,我得到“没有匹配的匹配函数来调用…” - VOID function passes array by reference, but when used, I get “No matching matching function for call to…” 如何通过引用未知大小的数组来调用函数? - How to call a function with a reference to an unknown size array? Function 和 0 arguments - void 还是 void*? - Function with 0 arguments - void vs void*? 如何在类/函数模板中传递void参数 - How to pass void arguments in a class/function templates 解包函数调用的参数数组 - Unpack an array of arguments for a function call 如何编写一个 void 函数来搜索数组中的值并将该值的位置通过引用传递回主函数? - How to write a void function that will search for values in an array and pass the position of that value back to main function by reference? 如何将“int”函数调用为“void”函数 - how to call a 'int' function to a 'void' function 如何将函数引用传递给参数 - How to pass function reference into arguments 是否存在一种优雅的解决方案来区分模板成员函数调用中的void参数和非void参数? - Is there an elegant solution to distinguish between void and non void arguments in a template member function call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM