hello,
I wanted to write a sort function which accepts arrays of different variable types.
Nonetheless I get compile errors, I can't find the reason:
void bubblesort(VTYPE *array, int length) {
^
sketch_mar08a:7: error: 'VTYPE' was not declared in this scope
sketch_mar08a:7: error: 'array' was not declared in this scope
void bubblesort(VTYPE *array, int length) {
^
sketch_mar08a:7: error: expected primary-expression before 'int'
void bubblesort(VTYPE *array, int length) {
^
exit status 1
variable or field 'bubblesort' declared void
what is wrong?
this is the code:
//---------------------------------------------------------
// Bubble Sort
//---------------------------------------------------------
template <typename VTYPE>
void bubblesort(VTYPE *array, int length) {
VTYPE tmp;
for (int i=0; i<length-1; i++) {
for (int j=0; j<length-i-1; j++) {
if (array[j] > array[j+1]) {
tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
}
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
function call in main/loop e.g.:
int intarray[200];
bubblesort(intarray, 200);
float floatarray[100];
bubblesort(floatarray, 100);
also if I change typename by class, the error remains...
I don't understand what that means. You say "this is the code", but then you show some function calls that aren't in the code. Does the error occur for you when you are compiling the minimal "this is the code" sketch?
The problem is with the way the Arduino IDE mangles your source file when it generates the function prototypes during the build process. Keep the declaration on one line and it should work:
template <typename VTYPE> void bubblesort(VTYPE *array, int length) {
FYI, the original code compiles fine with Arduino IDE 1.8.8. There are some cases where the Arduino IDE's prototype generation system fails, but the developers are steadily working to improve it.