[英]error: invalid conversion from 'int (*)[6]' to 'int' [-fpermissive]|
I have created a function that returns the sum of all the numbers in an array, however I keep getting an error message: error: invalid conversion from 'int (*)[6]' to 'int' [-fpermissive]. 我创建了一个函数,该函数返回数组中所有数字的总和,但是我不断收到错误消息:error:从'int(*)[6]'到'int'[-fpermissive]的无效转换。 In addition I also get an error that says: error: initializing argument 1 of 'int getTotal(int)'[-fpermissive]. 此外,我还收到一条错误消息:error:初始化'int getTotal(int)'[-fpermissive]的参数1。 It seems like these two errors go together. 似乎这两个错误并存。 Am I supposed to use pointers? 我应该使用指针吗? I have spent hours trying to figure this out with no luck. 我花了几个小时试图解决这个问题,但没有运气。
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int ROWS = 4;
const int COLS = 6;
void openInputFile(ifstream &,string);
int getTotal(int);
using namespace std;
int main()
{
int tot; //total of all numbers
int val;
int twoArray[ROWS][COLS];
ifstream inFile;
string inFileName = "nums.txt";
//Opening file
openInputFile(inFile, inFileName);
//Create 2D array
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
inFile >> twoArray[i][j];
}
}
//Close
inFile.close();
//THIS IS WHERE ERROR IS
tot = getTotal(twoArray);
printArray(twoArray);
return 0;
}
void openInputFile(ifstream &inFile, string theFile)
{
inFile.open(theFile.c_str());
if(!inFile)
{
cout << "Error opening the file!\n";
exit(13);
}
}
int getTotal(int array[][COLS])
{
int sum = 0;
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
sum+=array[i][j];
}
}
return sum;
}
int printArray(int array[][COLS])
{
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
return 0;
}
The problem is in wrong function declaration. 问题出在错误的函数声明中。 You have at the beginning of code: 您在代码开头:
int getTotal(int);
and then define function as: 然后将函数定义为:
int getTotal(int array[][COLS])
So, edit your functions' declaration. 因此,编辑函数的声明。 By the way I cannot see declaration for: 顺便说一下,我看不到以下声明:
int printArray(int array[][COLS]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.