Hello, I'm having an issue with a line of code that really doesn't seem that it should be giving me an error.
Before posting, I've googled this error and looked at related links with no help whatsoever. Most of the "solutions" were for correctly-placed brackets or different names (since some names were accidentally reused from the library, or were keywords). Anywho, here's the code--
#include <cstdlib>
#include <iostream>
#define TYPE(a) __typeof__(a)
#define ID(a) typeid(a).name()
#define ID_SAME(a,b) typeid(a).name() == typeid(b).name()
#define SWAP(a,b) TYPE(a) temp; temp = a; a = b; b = temp
#define PRINTLN(a) cout << a << endl
#define DO_SORT(varName, a, b, c, d) (*(varName)).sortValues<TYPE(a)>(a, b, c, d)
using namespace std;
template<class T>
class MySort
{
private:
public:
MySort(){};
~MySort(){};
void sortValues(T *value, int start, int size, void (*functocall)(void*, void*));
//This method accepts a user-defined function and sorts the value.
//The pointer is expected to have more than just one value, so it will be an array of some type.
bool isSorted(T *value, int start, int size)
{
for(int i = start; i < size - 1; i++)
{
if(value[i] > value[i + 1])
return false;
}
return true;
};
};
template<class T>
void MySort<T>::sortValues(T *value, int start, int size, void (*functocall)(void*, void*))
{
if(start < size)
{
while(!isSorted(value, start, size))
{
for(int i = start; i < size - 1; i++)
{
void *left = (value + start), *right = (value + (start + 1));
(*functocall)((left), (right));
}
}
}
else
PRINTLN("Start point cannot be greater than the size!");
}
void sortFunction(void *arg1, void *arg2)
{
if(ID(arg1) == ID(int) || ID(arg1) == ID(char))
{
if(arg2 < arg1)
{
SWAP(arg1, arg2);
}
}
}
int main(int argc, char *argv[])
{
int intArray[] = {9, 2, 5, 1, 7}, *intPt = intArray;
char charArray[] = {'o', 'a', 't', 'i', 'b'}, *charPt = charArray;
MySort<TYPE(charPt)> *myCharSorter = new MySort<TYPE(charPt)>();
DO_SORT(myCharSorter, charPt, 0, 5, sortFunction);
system("PAUSE");
return EXIT_SUCCESS;
}
--The error I get is this:
-> E:\RemakesAndNEwProjects\cplusplusfiles\p_SORt.cpp In function `int main(int, char**)':
->73 E:\RemakesAndNEwProjects\cplusplusfiles\p_SORt.cpp expected primary-expression before "typeof"
->73 E:\RemakesAndNEwProjects\cplusplusfiles\p_SORt.cpp expected `;' before "typeof"
-> E:\RemakesAndNEwProjects\cplusplusfiles\Makefile.win [Build Error] [p_SORt.o] Error 1
The reason I am creating this program is because I am a beginner at C++, carrying over a little knowledge from Java and I'm trying to emulate a generalized sorting program that sorts values stored in a pointer using a specified function argument (so that you can have different kinds of sorts done to values by simply defining the method of usage). I got the idea from a professor that told me that a class like this exists in the STL but I wanted to create my own version to gain an understanding of how void pointers and function pointers worked, as well as this class.
In any case, please let me know what the issue is. I can't find it and I've been trying for hours. Thank you.