简体   繁体   中英

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

Description Resource Path Location Type no matching function for call to 'minSortLoop(std::array*)' Test.cpp /gTest line 23 C/C++ Problem

How do I call my sort function correctly ?

best regards :-)

PS: I'm not allowed to use std::sort.


EDIT 1:

@François Moisan:

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

Description Resource Path Location Type no matching function for call to 'minSortLoop(std::array*)' Test.cpp /gTest line 23 C/C++ Problem

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

Description Resource Path Location Type no match for 'operator*' (operand type is 'std::array') Test.cpp /gTest line 23 C/C++ Problem

Not sure how to call this. The reference suggests something like my first example here .

@tadman:

I need to pass the size. That's given from the task description :-(

@Jarod42: Which compiler is this ? I'm using Cygwin in eclipse under windows 7.

@pasasap: Yes, I compiled it or at least I try. It results in the described errors.


EDIT 2:

As @pasasap mentioned in one of the comments, it seems like the problem is because of eclipse. Does anyone know a solution without turning off 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. Once I turned off the code analysis feature it compiled as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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