[英]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.