在C ++中将二维数组传递给函数

时间:2021-09-01 21:34:29

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

我正在尝试将二维数组传递给numOfStudents函数,但Visual Studio给出了一个错误说:

no instance of overloaded "numOfStudents" matches the argument list

没有重载“numOfStudents”的实例与参数列表匹配

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;
}

4 个解决方案

#1


4  

You declare numOfStudents with this argument list:

使用此参数列表声明numOfStudents:

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:

请注意,您的声明将char **作为第一个参数,但您的定义将其作为第二个参数(与int *相反)。参数的排序非常重要,必须完全匹配,因此您需要更改定义以匹配声明:

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

#2


0  

2D arrays can be a pain to pass around, why dont you use std::string and pass an array (or reference to std::vector) of them around?

2D数组可能是一个痛苦的传递,为什么你不使用std :: string并传递它们的数组(或引用std :: vector)?

#3


0  

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

检查每个参数的顺序。定义的第二个参数实际上是声明的第一个参数。如果你纠正订单,你应该很好

#4


0  

You have defined the signature of function numOfStudents as:

您已将函数numOfStudents的签名定义为:

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 ***---

}

#1


4  

You declare numOfStudents with this argument list:

使用此参数列表声明numOfStudents:

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:

请注意,您的声明将char **作为第一个参数,但您的定义将其作为第二个参数(与int *相反)。参数的排序非常重要,必须完全匹配,因此您需要更改定义以匹配声明:

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

#2


0  

2D arrays can be a pain to pass around, why dont you use std::string and pass an array (or reference to std::vector) of them around?

2D数组可能是一个痛苦的传递,为什么你不使用std :: string并传递它们的数组(或引用std :: vector)?

#3


0  

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

检查每个参数的顺序。定义的第二个参数实际上是声明的第一个参数。如果你纠正订单,你应该很好

#4


0  

You have defined the signature of function numOfStudents as:

您已将函数numOfStudents的签名定义为:

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 ***---

}