我有以下功能:
template <typename T, size_t SIZE>
void minSortLoop(array<T, SIZE>& a){
for(size_t o = 0; o < SIZE; o++) {
size_t minIx = 0;
for(size_t i = o + 1; i < SIZE; i++) {
if(a[i] < a[minIx]) {
minIx = i;
}
}
swap(a[o], a[minIx]);
}
}
我喜欢从其他位置调用它,例如:
std::array<int, 3> arr = {3,1,-9};
minSortLoop(arr);
但是我得到了错误:
Description Resource Path Location Type Invalid arguments ' Candidates are: void minSortLoop(? &) ' Test.cpp /gTest line 23 Semantic Error
Description Resource Path Location Type no matching function for call to 'minSortLoop(std::array*)' Test.cpp /gTest line 23 C/C++ Problem
如何正确调用排序函数?
最好的问候:-)
PS:我不允许使用 std::sort。
编辑 1:
@François Moisan:
我试过以其他方式通过,例如:
std::array<int, 3> arr = {3,1,-9};
minSortLoop(&arr);
有错误:
Description Resource Path Location Type Invalid arguments ' Candidates are: void minSortLoop(? &) ' Test.cpp /gTest line 23 Semantic Error
Description Resource Path Location Type no matching function for call to 'minSortLoop(std::array*)' Test.cpp /gTest line 23 C/C++ Problem
和:
std::array<int, 3> arr = {3,1,-9};
minSortLoop(*arr);
有错误:
Description Resource Path Location Type Invalid arguments ' Candidates are: void minSortLoop(? &) ' Test.cpp /gTest line 23 Semantic Error
Description Resource Path Location Type no match for 'operator*' (operand type is 'std::array') Test.cpp /gTest line 23 C/C++ Problem
不确定如何调用它。该引用资料建议类似于我的第一个示例 here .
@塔德曼:
我需要传递尺寸。这是从任务描述中给出的:-(
@Jarod42:这是哪个编译器?我在 Windows 7 下的 eclipse 中使用 Cygwin。
@pasasap:是的,我编译了它或者至少我试过了。它会导致所描述的错误。
编辑 2:
正如@pasasap 在其中一条评论中提到的,问题似乎是因为 eclipse 。有谁知道不关闭 cody-analysis 的解决方案?
最佳答案
应该是
std::array<int, 3> arr = {3,1,-9};
minSortLoop(arr); // not *arr or &arr
关于带 std::array 的 C++ 模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40643883/