c++ - Multiple definition, not in a header? Why does floor return a double? -
i tried program merge sort function using static arrays , of course have incomprehensible error.
so first question when compile program gives me multiple definition error on line 9 of code:
#include <cmath> #include "include\sort.h" /* theses fonctions accept arrays of unsigned short int length in unsigned int */ unsigned short int* sortbyfusion(unsigned short int arr[]) { // here !!!!!!!! const unsigned int arrsize = sizeof(arr)/sizeof(unsigned short int); if (arrsize <= 1) {return arr;} /* declarations , initializations of 2 half array */ const unsigned int arrleftsize = static_cast<unsigned int>(floor(arrsize/2)); const unsigned int arrrightsize = static_cast<unsigned int>(arrsize - arrleftsize); unsigned short int arrleft[arrleftsize]; unsigned short int arrright[arrrightsize]; (unsigned int = 0; < arrleftsize; ++) arrleft[i] = arr[i]; (unsigned int = 0; < arrrightsize; ++) arrright[i] = arr[i + arrleftsize]; /* sort 2 arrays */ (unsigned int = 0; < arrleftsize; ++) arrleft[i] = *(sortbyfusion(arrleft) + i); (unsigned int = 0; < arrrightsize; ++) arrright[i] = *(sortbyfusion(arrright) + i); /* , fusion them */ unsigned int i(0), j(0); (unsigned int k = 0; k < arrsize; k ++) { if (i >= arrleftsize) {arr[k] = arrright[j]; j ++;} //that line else if (j >= arrrightsize) {arr[k] = arrleft[i]; ++;} //and line here avoid segmentation fault else { if (arrleft[i] <= arrright[j]) {arr[k] = arrleft[i]; ++;} else {arr[k] = arrright[j]; j ++;} } } return arr; }
what did wrong? tried put ifndef define endif did nothing more. seems has problem multiple definition bit different on forum.
secondly, used static_cast why floor return double when pass double argument? logically, should give integer (the floor of number integer ...)?
for compiler, gnu gcc, don't know how find version. , work code::blocks.
and header file :
#ifndef sort_h_included #define sort_h_included unsigned short int* sortbyfusion(unsigned short int arr[]); #endif // sort_h_included
const unsigned int arrsize = sizeof(arr)/sizeof(unsigned short int);
this line not working expected, because c++ doesn't keep information length of array runtime array - pointer array, sizeof(arr)
returns sizeof(unsigned short int*)
size of pointer.
as why there error in line 9, can't without seeing header sort.h
.
//static_cast<unsigned int>(floor(arrsize/2)); // wrong arrsize/2; // right, c++ floor integer divisions.
unsigned short int arrleft[arrleftsize]; unsigned short int arrright[arrrightsize];
these 2 lines aren't valid c++ because length of declared arrays can not determined @ compile time.
Comments
Post a Comment