【问题标题】:Passing two dimensional array into function in C++将二维数组传递给C++中的函数
【发布时间】:2013-01-05 16:16:16
【问题描述】:

我正在尝试将二维数组传递给 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;
}

【问题讨论】:

  • 这几乎不是 C++,但充其量是 C 使用一些(非常次要的)C++ 功能。在适当的 C++ 中,您将使用 std::vector&lt;student&gt; 或类似的。

标签: c++ arrays function dimensional


【解决方案1】:

您使用此参数列表声明 numOfStudents:

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

但是你用不同的参数列表定义它:

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

请注意,您的声明将char** 作为第一个参数,但您的定义将它作为第二个参数(int* 则相反)。参数的顺序很重要,必须完全匹配,所以你需要改变你的定义来匹配声明:

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

【讨论】:

    【解决方案2】:

    二维数组可以是pain to pass around,为什么不使用std::string 并传递它们的数组(或引用std::vector)?

    【讨论】:

      【解决方案3】:
      int unsigned numOfStudents(char **namesArray, int *, FILE *);  // declaration
      
      int unsigned numOfStudents(int *marksArray, char **namesArray, FILE *file) // definition
      

      检查每个参数的顺序。定义的第二个参数实际上是声明的第一个参数。把订单改正就好了

      【讨论】:

        【解决方案4】:

        您已将函数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 ***---
        
        }
        

        【讨论】:

          猜你喜欢
          • 2021-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          相关资源
          最近更新 更多