我可以在函数外运行第一个部分,但当我尝试在函数中使用ifstream对象运行代码时,我会收到以下错误:
operator>>
不匹配(操作数类型为std::ifstream
(也称为std::basic_ifstream<char>
}和const int
(
常量和标头
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int COLS = 5; //Declaring constant number of cols
const int ROWS = 7; //Declaring constant number of rows
const string FILENAME = "foodDrive.txt";
//Function Prototypes
以下作品:
int main()
{
int food[ROWS][COLS] = {0}; //declaring 2D array that will hold values
ifstream inFile;
inFile.open(FILENAME);
for (int r = 0; r < ROWS; r++) //Outer loop for rows
{
for (int c = 0; c < COLS; c++) //inner loop for columns
{
inFile >> food[r][c]; //Take input from file and put into food
}
}
inFile.close();
但这不是
void readArray(const int ary[][COLS],int rows)
{
ifstream inFile;
inFile.open(FILENAME);
for (int r = 0; r < rows; r++) //Outer loop for rows
{
for (int c = 0; c < COLS; c++) //inner loop for columns
{
inFile >> ary[r][c]; //Take input from file and put into food
}
}
inFile.close();
}
删除函数头中的const
,我想这应该会有所帮助:
void readArray(int ary[][COLS], int rows) { ...