带 std::array 的 C++ 模板函数

标签 c++ arrays c++11 templates

我有以下功能:

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/

相关文章:

c++ - 如何在 C++ 中获取使用我的服务的用户的 SID 和用户名

c++ - C++除法精度不够如何提高结果精度

c++ - 如果附加了 gdb,我该如何中断,但如果没有附加,我该如何继续?

javascript - 您可以在 javascript 中使用模板文字创建对象属性名称吗?

C++指针练习

javascript - 如何迭代整个数组?然后更新数据库

php - 用于日历的 mySQL 的 while 循环中的 json_encode() 数组

c++ - libc++ 无法处理 fcgi streambuf

c++ - 分配器感知容器和 propagate_on_container_swap

c++ - vector 、 move 语义、nothrow 和 g++ 4.7