1

関数に2 次元配列を渡そうとしていnumOfStudentsますが、Visual Studio で次のようなエラーが表示されます。

オーバーロードされた「numOfStudents」のインスタンスが引数リストと一致しません

私はすべてを試してきましたが、解決策が見つかりません。

#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

4 に答える 4

4

次の引数リストを使用して numOfStudents を宣言します。

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

ただし、別の引数リストを使用して定義します。

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

宣言には がchar**最初の引数として含まれていますが、定義には 2 番目の引数として含まれていることに注意してください ( の場合は逆ですint*)。引数の順序は非常に重要であり、正確に一致する必要があるため、宣言に一致するように定義を変更する必要があります。

int unsigned numOfStudents(char **namesArray, int *marksArray, FILE *file)
{
....
}
于 2013-01-05T16:17:34.663 に答える
0
int unsigned numOfStudents(char **namesArray, int *, FILE *);  // declaration

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

それぞれのパラメータの順序を確認してください。定義の 2 番目のパラメーターは、実際には宣言の 1 番目のパラメーターです。順番を直せば大丈夫

于 2013-01-05T18:02:07.943 に答える
0

関数のシグネチャを次のように定義しましたnumOfStudents

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

関数を次のように宣言します。

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

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

}

これは、関数のシグネチャと関数宣言の「パラメータを呼び出す順序」が、パラメータの受け渡しと同じでなければならないという問題です。したがって、次のようになります。

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

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

}
于 2013-08-29T09:31:45.943 に答える
0

2D 配列は渡すstd::stringのが面倒な場合があります。それらの配列 (またはへの参照)を使用して渡しstd::vectorませんか?

于 2013-01-05T16:17:50.633 に答える