Alex Alex - 10 days ago 6
C++ Question

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 ?

Answer

The problem is my IDE Eclipse. Once I turned off the code analysis feature it compiled as expected.

Comments