C++ 代码错误

C++ Error with code

本文关键字:错误 代码 C++      更新时间:2023-10-16

我目前正在学习C++,并且有一个使用此代码的程序可以正常工作。我将这段代码从我的第一个程序复制到我的第二个程序中,但我无法弄清楚导致错误的原因。错误是"错误:StudentResults的参数类型与StudentResults的参数类型不兼容

代码如下,我知道其中一些被引用出来,这只是为了阻止某些事情工作,直到我得到这个错误排序。这只是我认为错误所在的代码示例,如果需要更多代码,我将添加其余代码。错误是****的位置,这些实际上并不在代码中,只是为了突出显示错误的位置。谢谢

//Prototypes - common
void press_key(HANDLE hdl, int col, int row);
void message(HANDLE hdl,  char mess[], int col, int row);
char again(HANDLE hdl, int col, int row);
void set_menu(HANDLE hdl );
int  get_option(HANDLE hdl, int c , int r , int min , int max);
void gotoXY( HANDLE hdl, SHORT x, SHORT y );
void clrScr(HANDLE hdl );
void getscreen(HANDLE hdl, char name[]);
void validateString(HANDLE hdl, int col, int row, int min, int max,char str[], char errMessage[]);
int validateNumber(HANDLE hdl, int col, int row, int min, int max, char errMessage[]);
//prototypes - menu
int  populateStock( struct   StudentDetails   student_details[]);
void reportResults(HANDLE hdl, struct StudentDetails student_details[], int size);
void displayBySurname(HANDLE hdl,struct StudentDetails student_details[], int  size);
void displayByStudentNumber(HANDLE hdl, struct StudentDetails student_details[30], int size);
void updateStock(HANDLE hdl, struct StudentDetails student_details[], int size );
void addStock(HANDLE hdl,  struct StudentDetails student_details[], int & size);
struct StudentResults
{
int candidateNo;
char forename[20], surname[20];
int section[5];
};

int _tmain(int argc, _TCHAR* argv[])
{
// insert HANDLE hdl
HANDLE hdl = GetStdHandle( STD_OUTPUT_HANDLE );
    //Data Declaration
    int option, size;
    struct StudentResults student_details[30] ;
    // fill array with some stock details
    size = populateStock(student_details);   // error here
    do
    {
        set_menu(hdl);
        option=get_option(hdl, 44,20,1,7);      
        switch(option)
        {
             case 1:;  //reportStock(hdl, student_details, size);
             case 2:;  //displayStockByCat(hdl, student_details, size); 
             case 3:;  //searchStock(hdl, stock_details, size);
             case 4:;  //searchStockByDesc(hdl, stock_details, size);
             case 5:;  //updateStock(hdl, stock_details,size);
             case 6:; //addStock(hdl, stock_details,size);
        }
    } while (option !=7);
    return 0;
}
int populateStock( struct StudentResults student_details[])
{
    int size;
    student_details[0].candidateNo  =  1004;    
    strcpy_s(student_details[0].forename , "Joe");          
    strcpy_s(student_details[0].surname , "Bloggs"); 
    student_details[0].section[0] = 20.87;
    student_details[0].section[1] = 20.87;
    student_details[0].section[2] = 20.87;
    student_details[0].section[3] = 20.87;
    student_details[0].section[4] = 20.87;

return 1;
}

这意味着,作为populateStock参数传递的内容不是它所采用的参数类型。它需要不同类型的参数。由于您没有发布函数代码populateStock我无法准确地指出您,但这是我能告诉您的最好的。

从你的函数populateStock定义:

int  populateStock( struct   StudentDetails   student_details[]);

populateStock接受struct StudentDetails作为参数类型,但您向其传递一个类型为 struct StudentResults 的对象,这是一个不兼容的类型。

struct StudentResults student_details[30] ;
// fill array with some stock details
/*This will generate an error as your passing an incompatible type */
size = populateStock(student_details);