2013-01-05 111 views
1

我想传递一个二维数组到numOfStudents功能,但Visual Studio是给我一个错误说:传递二维数组功能在C++

没有超载“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; 
} 
+0

这几乎不是C++,但最好是C使用一些(非常小的)C++功能。在适当的C++中,你可以使用'std :: vector '或类似的东西。 – Walter

回答

4

你这个参数列表声明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) 
{ 
.... 
} 
0

二维数组可以是pain to pass around,您使用std::string为什么不和传递一个数组(或参考std::vector)他们身边?

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

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

检查每个参数的顺序。定义的第二个参数实际上是声明的第一个参数。

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

,你声明的功能:如果你纠正

0

您已经定义的函数numOfStudents签名的顺序应该是不错的

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

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

} 

这就是问题的函数签名和函数声明的“调用参数序列”应与传递参数相同。所以它应该是这样的:

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

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

}