"no matching function call"模板化函数项目
"no matching function call" with templated functions project
我正在编写一个程序来生成和排序一个随机数数组。编译器给我以下错误:
select.cxx:在函数"void selectionsort(项目*,尺寸类型)"中[withItem=int,SizeType=long unsigned int]':select.ccxx:95:从这里实例化select.cx:16:错误:调用"swap(int*&;,long unsigned int&;,long unsignedint&;)"时没有匹配的函数
这是我的代码:
#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);
}
我有一种感觉,这个问题与传递给交换函数的数据类型有关。当在main()
中声明的n
的数据类型是size_t
时,我还困惑于为什么错误的第一行表示SizeType = long unsigned int
。
确保在模板函数中调用的其他函数可见。因此,在swap()
和index_of_minimal()
之后定义selection_sort()
。
附带说明:
int rNumbers[n]; //declares int array of size n
声明了一个可变大小的数组,这不是标准C++(虽然有些编译器支持它,但您不应该依赖它)。如果需要运行时大小的数组,请改用std::vector
。
关于最后的混淆,size_t
是一个类型别名,在您的实现中,它恰好是unsigned long int
的别名,所以这就是错误提到它的原因。
相关文章:
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 我可以在Visual Studio 2019上的项目下使用多个主函数吗?
- 如何将我的小程序转换为用于项目的函数?
- 阻止我的项目调用__CxxFrameHandler3(CRT函数)
- CMake 子项目不能调用 OpenGL 函数,但父项目可以
- 将项目插入 CTreeCtrl 时线程函数的奇怪行为
- 从另一个动态链接库项目调用静态库函数
- 如何从 DLL 项目调用 C# 项目函数C++
- 在C++项目中管理自由函数有哪些好的做法?
- 优点和缺点 在类内为大型项目定义的内联朋友助手免费函数
- 如何从Qt C++项目运行Python函数
- 如何在VS Code中创建C++项目并链接主,函数和标题?
- 如何在项目 2 中使用项目 1 中的函数
- 如何创建一个函数,该函数可以从一个qtreewidget中删除项目并将其添加到另一个函数
- 类介绍 (c++) 项目希望我们创建两个构造函数,但它们都不需要任何参数 - 我应该在这里做什么?
- 将函数从控制台应用程序移动到共享库项目似乎会带来不相关的编译错误
- 默认构造函数 Eclipse 项目中的用户输入
- NDK - 包含一个预构建的库,并在 Android 项目中调用本机函数
- 添加项目后C find()函数不起作用
- "no matching function call"模板化函数项目