繁体   English   中英

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

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

晚上好,我试图在main中调用我的void函数“ getProblems”,但是在不带参数的情况下输出“ getProblems”时会得到一个无关的值。 类似地,当传递参数时,例如“ getProblems(list,i)”,我收到错误消息“没有运算符'<<'与这些操作数匹配”。 目的是在不使用返回值的函数或指针的情况下输出文本文件包含的问题数。

#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();
}

您的函数getProblems()的类型为void ,那么您要使用cout显示什么?

如果您需要显示计数(这是一个参数),

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM