与模板化函数项目“没有匹配的函数调用”

时间:2015-11-25 05:00:12

标签: c++ templates

我正在编写一个程序来生成和排序随机数组。 编译器给出了以下错误:

  

select.cxx:在函数'void selectionsort(Item *,SizeType)中   [withItem = int,SizeType = long unsigned int]':           select.cxx:95:从这里实例化           select.cxx:16:错误:没有匹配函数来调用'swap(int *&,long unsigned int&,long unsigned int&)'

这是我的代码:

#include <cassert>
#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

template <class Item, class SizeType>
void selectionsort(Item data[], SizeType n)
{
    for (SizeType i = 0; i = n - 2; i++)
    {
        SizeType j = index_of_minimal(data, i, n);
        swap(data, i, j); //data[i] swapped with data[j](minimum)
    }
}

template <class Item, class SizeType>
std::size_t index_of_minimal(const Item data[], SizeType i, SizeType n)
{
    size_t min = i; //holds index of minimum (initialized to i)
    Item t1 = data[i];  //temporary holder for comparing values, initialized as i (starting value)
    Item t2;        //second holder

    for (SizeType j = i++; j = n - 1; j++)
    {
        t2 = data[j];

        if (t2 < t1)
        {
            t1 = data[j];
            min = j;
        }
    }

    return min;
}

template <class Item, class SizeType>
void swap(Item data[], SizeType i, SizeType j) //data[i] swapped with data[j](minimum)
{
    Item temp; //holds value to be swapped
    temp = data[i];
    data[i] = data[j];
    data[j] = temp;
}

template <class Item, class SizeType>
void listPrint(Item data[ ], SizeType n)
{
    cout << "array:";

    for (SizeType i = 0; i = n - 1; i++)
    {
        cout << " " << data[i];
    }

    cout << endl;
}

int myrand(int lower, int upper)
{
    return (lower + rand() % ( upper - lower + 1 )  );
}

int main()
{
    size_t n; //user input

//For random number generator//
    srand(time(NULL));

    cout << "Please enter a number:" << endl;
    cin >> n;

    while (n < 1)
    {
        cout << "Error: please enter a number 1 or larger" << endl;
        cin >> n;
    }

    int rNumbers[n]; //declares int array of size n
    int randomN;    //to hold randomly generated number

    for (size_t i = 0; i < n; i++)
    {
        randomN = myrand(1, 1000); //generates a random number as randomN
        rNumbers[i] = randomN;
    }

    cout << "Unsorted ";
    listPrint(rNumbers, n);
    selectionsort(rNumbers, n);
    cout << "Sorted ";
    listPrint(rNumbers, n);

}

我感觉问题与传递给swap函数的数据类型有关。当SizeType = long unsigned int中声明的n的数据类型为main()时,我也很困惑为什么错误的第一行指出size_t

1 个答案:

答案 0 :(得分:1)

确保您在模板功能中调用的其他功能可见。因此,在 if ($rowR['accept'] == "Accepted") { echo "<h3 style='color:#001F7A;'><b>You Have Updates </b><i class='fa fa-bell-o'></i></h3>"; echo $rowR['bidder_id']; $recR = "SELECT users_id, first_name, last_name FROM tbl_users WHERE users_id = '" . $rowR['bidder_id'] . "'"; $recResultB = mysql_query($recR, $con)or die(mysql_error()); while ($rowre = mysql_fetch_array($recResultB)) { echo " <tr><td>" . $rowre['first_name'] . " " . $rowre['last_name'] . "</td></tr>"; // echo $rowre['users_id']; } } selection_sort()之后定义swap()

旁注:

index_of_minimal()

声明一个变量大小的数组,这不是标准的C ++(虽然有些编译器支持它,但你不应该依赖它)。如果您需要运行时大小的数组,请改用std::vector

关于最后的混淆,int rNumbers[n]; //declares int array of size n 是一个类型别名,在您的实现中恰好是size_t,因此错误提到它的原因。