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.

sortValues is not a template member. you need to modify DO_SORT

#define DO_SORT(varName, a, b, c, d) (*(varName)).sortValues(a, b, c, d)

when you are sorting an array of char , use MySort<char> not MySort<char*>

//...
    MySort<char> *myCharSorter = new MySort<char>();
    DO_SORT(myCharSorter, charPt, 0, 5, sortFunction);
//...

using all these void* (java object?), preprocessor macros and non-standard extensions (__typeof__) is not good C++.
here is your program translated into C++ (with a minor bug fixed).

#include <cstdlib>
#include <iostream>
using namespace std;

template<class T>
class MySort
{
      private:

      public:
             MySort(){};
             ~MySort(){};
             void sortValues( T* value, int start, int size,
                              void (*functocall)(T*, T*));
             //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( const 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)(T*, T*))
{
    if(start < size)
    {
       while(!isSorted(value, start, size))
       {
            for(int i = start; i < size-1 ; i++)
            {
               T *left = value+i, *right = value+i+1 ;
               (*functocall)((left), (right));
            }
       }
    }
    else
       std::cerr << "Start point cannot be greater than the size!" ;
}

template< typename T > void sortFunction( T* arg1, T* arg2 )
{
  if( *arg2 < *arg1 )
  {
    T temp = *arg1 ;
    *arg1 = *arg2 ;
    *arg2 = temp ;
  }
}

int main(int argc, char *argv[])
{
    enum { SZ = 5 };
    int intArray[SZ] = {9, 2, 5, 1, 7} ;
    char charArray[SZ] = {'o', 'a', 't', 'i', 'b'} ;

    MySort<char> myCharSorter ;
    myCharSorter.sortValues( charArray, 0, SZ, &sortFunction<char> ) ;
    std::cout << "sorted? " << std::boolalpha
              << myCharSorter.isSorted( charArray, 0, SZ ) << '\n' ;

    MySort<int> myIntSorter ;
    myIntSorter.sortValues( intArray, 0, SZ, &sortFunction<int> ) ;
    std::cout << "sorted? "
              << myIntSorter.isSorted( intArray, 0, SZ ) << '\n' ;
}
commented: Much better :) +17

Hey thanks a million for the code-edit, but I was really hoping for an explanation of why I'm receiving the error.

The reason I use preprocessor macros is because it really short-lines long winded code. I didn't realize that using __typeof__(element) was a nonstandard c++ convention.

I thought the way Java defined instanceof was based off of __typeof__(e) which is the reason I thought there would be no harm in using it to label the actual type of the element for easier macroing.

I'm actually greatful that you took the time to edit the code, but I really wish for some type of explanation for my elusive error...

I also can't believe how badly I goofed with using char* instead of char... No wonder the compiler was looking for a char** in some occassions, it was replacing T *value with char **value and expecting that type of argument.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.