C++ C++;错误:";没有要调用的匹配函数;使用argc、*argv[]和多维数组调用void函数时
我试图调用一个函数将.pgm图片文件读入数组,但无法从int main()调用该函数。所有参数都在那里,函数调用是编译器发现的唯一错误。这是用C++编写的。C++ C++;错误:";没有要调用的匹配函数;使用argc、*argv[]和多维数组调用void函数时,c++,arrays,function,C++,Arrays,Function,我试图调用一个函数将.pgm图片文件读入数组,但无法从int main()调用该函数。所有参数都在那里,函数调用是编译器发现的唯一错误。这是用C++编写的。 //this program reads a .pgm file into an array, then prints a copy to an output file. //right now i'm just trying to make the program call the read function and properly /
//this program reads a .pgm file into an array, then prints a copy to an output file.
//right now i'm just trying to make the program call the read function and properly
//read the data from the input file
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//the maximum size of the photo to be stored in the array
const int rows = 512;
const int columns = 512;
//function prototype; the multidimensional array's row value has to be passed as a parameter.
//the intoArray function opens the input file (a .pgm file), reads the amount of rows and columns,
//then reads the pixel data into a 512x512 array and closes the input filestream
void intoArray (int argc, char* argv[], int photoArray[][columns], int &inputRows, int &inputColumns, int rows);
int main(int argc, char* argv[]) {
int photoArray, inputRows, inputColumns, rows;
ifstream pgmIn;
ofstream pgmOut;
if (argc != 3) {
cout<<"Error: Incorrect number of parameters. "<<endl;
return -1;
}
intoArray (argc, argv, photoArray, inputRows, inputColumns, rows);//error: no matching function to call to 'intoArray'
//just testing for proper fileread
cout<<"rows: "<<inputRows<<endl
<<"columns: "<<inputColumns<<endl;
return 0;
}
void intoArray (int argc, char *argv[], int photoArray[][columns], int &inputRows, int &inputColumns, int rows) {
ifstream pgmIn;
string p2;
int twofivefive, i, j;
if (argc != 3)
cout<<"Error: Incorrect number of parameters. "<<endl;
//open the input filestream
pgmIn.open(argv[1]);
//check if file opened properly
if (pgmIn.fail()) {
perror (argv[1]);
}
//read the initial data from the pgm file. the p2 and twofivefive variables
//can be ignored, as they are not needed, but need to be read in order to get to the
//actual picture data.
while (pgmIn>>p2>>inputColumns>>inputRows>>twofivefive) {
//once again just checking for proper fileread.
//i'm not sure what i did, but before the function became impossible to call,
//only the p2 string was being read and it printed just fine, but the rest of
//the values were never read.
cout<<p2<<" "<<endl
<<inputColumns<<" "<<inputRows<<" "<<twofivefive;
//this actually reads the picture data into the array.
for (i = 0; i < inputRows; i++) {
for (j = 0; j<inputColumns; i++) {
pgmIn>>photoArray[i][j];
}
}
}
pgmIn.close();
}
//此程序将.pgm文件读入数组,然后将副本打印到输出文件中。
//现在我只是想让程序调用read函数
//从输入文件中读取数据
#包括
#包括
#包括
使用名称空间std;
//要存储在阵列中的照片的最大大小
const int rows=512;
const int columns=512;
//功能原型;多维数组的行值必须作为参数传递。
//intoArray函数打开输入文件(一个.pgm文件),读取行和列的数量,
//然后将像素数据读入512x512数组并关闭输入文件流
void intoArray(int-argc,char*argv[],int-photoArray[][列],int&inputRows,int&inputColumns,int行);
int main(int argc,char*argv[]){
int photoArray、inputRows、inputColumns、rows;
ifpgmin;
流pgmOut;
如果(argc!=3){
cout您的参数不匹配。您正在传递一个int(photoArray
),其中函数需要一个int[][columns]
(二维数组)
您将收到一个“无需调用的匹配函数”错误,因为链接器找不到void intoArray(int,char*[],int,int&,int&,int&,int&,int&,int);
的匹配函数签名,当您打算调用void intoArray(int,char*[],int[][列],int&,int&,int&,int);
时,这就是您正在调用的函数签名
因此,您应该将photoArray声明为二维数组,以匹配intoArray所期望的类型:
int photoArray[rows][columns];
int inputRows, inputColumns, rows;
==========
实际上,正如Matt指出的,在<代码>主< <代码>函数中,不应该有<代码>行> /Cord>变量,因为您已经得到了<代码>行>代码>全局变量。
< p>我对C++是很新的,所以我不确定是否可以完全帮助您。但是,我将指出我所看到的一些错误:-< /p>
函数原型中的参数是2d int数组,类似于int photoarray[][],但是当您在主函数中调用该函数时,您的参数不是2d数组。photoarray只是一个int数组,而不是2d数组。它应该是一个二维数组,int photoarray[行][列]
行
和列
是全局常量;main
中的局部变量行
不应该在那里。非常感谢Matt。这样做了。哇!回答得太快了。谢谢@Mattmcnab。