简体   繁体   中英

Error templates with functions in C++

I have a problem with the calling to the function. After I debug it said that 'boobleSort : undeclared identifier' and 'type short unexpected'.

main()
{
  short arr[] = {2,10,-15,20};
  short size = 4;
  bobbleSort<short, short*>(size, arr);
}

template<class FIRST_TYPE, class SECOND_TYPE>
void bobbleSort(FIRST_TYPE size, SECOND_TYPE arr)
{
  arr[2]++;
}

How can I fix it?

Make sure to return 0 from the main. This compiles and runs for me. Make sure to add some error checking to the bobbleSort since the arr may not have 3 elements.

template<class FIRST_TYPE, class SECOND_TYPE>
void bobbleSort(FIRST_TYPE size, SECOND_TYPE arr)
{
    arr[2]++;
}

int main()
{
    short arr[] = { 2,10,-15,20 };
    short size = 4;
    bobbleSort(size, arr);

    return 0; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM