简体   繁体   中英

Passing two dimensional array into function in C++

I'm trying to pass a two-dimensional array to the numOfStudents function, but Visual Studio is giving me an error saying:

no instance of overloaded "numOfStudents" matches the argument list

I've been trying everything and I can't find a solution.

#include <stdio.h>
#include <cstring>

//Prototypes
int unsigned numOfStudents(char **namesArray, int *, FILE *);
void printArrays(char **namesArray, int *marksArray, int loopCounter);
int unsigned minMark(int);
int unsigned maxMark(int);
int unsigned averageMark(int);

//Two constants used later to create array of characters
const int MAX_SIZE = 10;
const int NAME_LENGTH = 30;

int main()
{
    // Declaring array which will hold all names from file
    char namesArray[MAX_SIZE][NAME_LENGTH];
    int unsigned studentNumber = 0;
    int loopCounter = 0;
    int marksArray[MAX_SIZE]; //Array will hold the marks of students, it needs to be same size as the previous array

    FILE *file; //creating pointer to a file
    file = fopen("names.txt", "r"); //telling the pointer where the file is and how to access it

    loopCounter = numOfStudents(namesArray, marksArray, file);
    //System pause - will hold console window open
    getchar();
    return 0;
}

int unsigned numOfStudents(int *marksArray, char **namesArray, FILE *file)
{
    char tempArrayName[50]; //Temporary array to hold names
    char tempArrayLastName[50]; //Temporary array to hold last names
    int i = 0; //Counter
    bool stop = false;

    if(file == NULL)
    {
        printf("\nFile has been not opened correctly\n");
    }
    else
    {
        while (stop == false)
        {
            //Reading the file in order to get 3 separate lines which are assumed as a single record of one student
            fscanf(file, "%s\n%s\n%d", tempArrayName, tempArrayLastName, &marksArray[i]);
            //Following lines are compying strings from temp arrays into main array and adding a space for better readability
            strcpy(namesArray[i], tempArrayName);
            strcat(namesArray[i], " ");
            strcat(namesArray[i], tempArrayLastName);
            //Chcecking if the sentinel value was read in order to stop the loop
            stop = strcmp(tempArrayName, "****");
            i++; //adding the counter
            //Checking if file is too big, before program will crash with an internal error
            if (i == MAX_SIZE)
            {
                printf("ERROR!!! FILE TOO BIG TO READ!");
                stop == true;
            }
        }
    }

    return i;
}

You declare numOfStudents with this argument list:

int unsigned numOfStudents(char **namesArray, int *, FILE *);

But then you define it with a different argument list:

int unsigned numOfStudents(int *marksArray, char **namesArray, FILE *file)

Note that your declaration has the char** as the first argument, but your definition has it as the second argument (and the opposite for the int* ). The ordering of the arguments is very important and must match exactly, so you need to change your definition to match the declaration:

int unsigned numOfStudents(char **namesArray, int *marksArray, FILE *file)
{
....
}

2D数组可能很难传递 ,为什么不使用std::string并传递它们的数组(或引用std::vector )?

int unsigned numOfStudents(char **namesArray, int *, FILE *);  // declaration

int unsigned numOfStudents(int *marksArray, char **namesArray, FILE *file) // definition

Check the order of the parameters in each. The definition's second param is actually declaration's 1st param. You should be good if you rectify the order

You have defined the signature of function numOfStudents as:

int unsigned numOfStudents(char **namesArray, int *, FILE *);

and you declaring the function as :

int unsigned numOfStudents(int *marksArray, char **namesArray, FILE *file)
{

      ---*** Your Code Here ***---

}

This is the problem that the "sequence of calling parameter" of the signature of the function and the function declaration should be same as the passing parameter. So it should be like :

int unsigned numOfStudents(char **namesArray, int *marksArray, FILE *file)
{

      ---*** Your Code Here ***---

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM