TAGS :Viewed: 7 - Published at: a few seconds ago

[ C++ Error: "no matching function to call to " when calling a void function with argc, *argv[], and a multidimensional array ]

I'm trying to call a function to read a .pgm picture file into an array but the function is not able to be called from int main(). All of the parameters are there, and the function call is the only error the compiler is finding. This is written in 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
//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();
}

Answer 1


Your parameters don't match. You're passing an int (photoArray) where the function wants an int[][columns] (a 2-dimensional array).

You're getting a "no matching function to call" error because the linker can't find a matching function signature for void intoArray (int, char*[], int, int&, int&, int);, which is what you're calling, when you meant to be calling void intoArray (int, char*[], int[][columns], int&, int&, int);.

So, you should instead have declared photoArray as a 2-dimensional array to match the type expected by intoArray:

int photoArray[rows][columns];
int inputRows, inputColumns, rows;

==========

And actually, as Matt points out below, you shouldn't have the rows variable in the main function because you've already got the rows global variable.

Answer 2


I am pretty new to c++ so I am not sure if I can totally help you. But, I will point out some of the bugs I have seen:-

Argument in your functions prototype is 2d int array like int photoarray[][] but when you called the function in the main; your argument is not 2d array. Photoarray is just an int, not a 2d array.