简体   繁体   English

具有std :: array的C ++模板函数

[英]C++ template function with std::array

I have the following function: 我有以下功能:

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]);
    }
}

I like to call it from the other location like: 我喜欢从其他位置调用它:

std::array<int, 3> arr = {3,1,-9};
minSortLoop(arr);

But I get the errors: 但是我得到了错误:

Description Resource Path Location Type Invalid arguments ' Candidates are: void minSortLoop(? &) ' Test.cpp /gTest line 23 Semantic Error 说明资源路径位置类型无效的参数'候选者为:void minSortLoop(?&)'Test.cpp / g测试行23语义错误

Description Resource Path Location Type no matching function for call to 'minSortLoop(std::array*)' Test.cpp /gTest line 23 C/C++ Problem 说明资源路径位置类型无匹配函数可调用'minSortLoop(std :: array *)'Test.cpp / g测试行23 C / C ++问题

How do I call my sort function correctly ? 如何正确调用排序函数?

best regards :-) 最好的祝福 :-)

PS: I'm not allowed to use std::sort. PS:我不允许使用std :: sort。


EDIT 1: 编辑1:

@François Moisan: @弗朗索瓦·莫伊桑(FrançoisMoisan):

I've tried to pass in other ways like: 我试图以其他方式通过:

std::array<int, 3> arr = {3,1,-9};
minSortLoop(&arr);

with error: 错误:

Description Resource Path Location Type Invalid arguments ' Candidates are: void minSortLoop(? &) ' Test.cpp /gTest line 23 Semantic Error 说明资源路径位置类型无效的参数'候选者为:void minSortLoop(?&)'Test.cpp / g测试行23语义错误

Description Resource Path Location Type no matching function for call to 'minSortLoop(std::array*)' Test.cpp /gTest line 23 C/C++ Problem 说明资源路径位置类型无匹配函数可调用'minSortLoop(std :: array *)'Test.cpp / g测试行23 C / C ++问题

and: 和:

std::array<int, 3> arr = {3,1,-9};
minSortLoop(*arr);

with error: 错误:

Description Resource Path Location Type Invalid arguments ' Candidates are: void minSortLoop(? &) ' Test.cpp /gTest line 23 Semantic Error 说明资源路径位置类型无效的参数'候选者为:void minSortLoop(?&)'Test.cpp / g测试行23语义错误

Description Resource Path Location Type no match for 'operator*' (operand type is 'std::array') Test.cpp /gTest line 23 C/C++ Problem 说明资源路径位置类型与'operator *'不匹配(操作数类型为'std :: array')Test.cpp / g测试行23 C / C ++问题

Not sure how to call this. 不知道如何称呼它。 The reference suggests something like my first example here . 参考文献建议类似我在这里的第一个示例。

@tadman: @tadman:

I need to pass the size. 我需要通过大小。 That's given from the task description :-( 这是从任务描述中给出的:-(

@Jarod42: Which compiler is this ? @ Jarod42:这是哪个编译器? I'm using Cygwin in eclipse under windows 7. 我在Windows 7下的Eclipse中使用Cygwin。

@pasasap: Yes, I compiled it or at least I try. @pasasap:是的,我编译了它,或者至少尝试了。 It results in the described errors. 导致描述的错误。


EDIT 2: 编辑2:

As @pasasap mentioned in one of the comments, it seems like the problem is because of eclipse. 正如@pasasap在评论之一中提到的那样,问题似乎出在日食。 Does anyone know a solution without turning off cody-analysis ? 没有人知道解决方案而不关闭cody-analysis吗?

It should simply be 应该只是

std::array<int, 3> arr = {3,1,-9};
minSortLoop(arr); // not *arr or &arr

The problem is my IDE Eclipse. 问题是我的IDE Eclipse。 Once I turned off the code analysis feature it compiled as expected. 一旦我关闭了代码分析功能,它就会按预期进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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