Passing a non-static method to a Generic Function

65 Views Asked by At

I have a header file where one of the functions is:

template <class Pclass, class Pfunc1, class Pfunc2>  //Capable of multiple outputs per call
int view_recordT(Pclass Object_1, char Compare_char, char* file_1, Pfunc1 show_tablular, Pfunc2 get_compare_char) {   //Search function to find records
    int currID, records_read;
    bool run=false;
    ifstream fin;
    fin.open(file_1, ios::binary);   //Opens the file again
    if(!fin) {     //Checks whether the file file_1 exists or not
        cout<<"File not found!";
        return(-1);
    }

    while(fin>>currID && fin.read((char*)& Object_1, sizeof(Object_1)))  //Reads a record into stream until it can't
    if(Object_1.get_compare_char() == Compare_char) {
        records_read++;
        cout<<endl; AddColumn(currID,7); Object_1.show_tablular();
        run=true;
    }
    fin.close();
    if(run==false)   //Returns a false flag (0) if records not found
        return int(run);
    return records_read;  //Otherwise, the number of records read is returned
}

Note: bool is an enum as TurboC++ doesn't support boolean values, true/false. enum bool { false, true }

I've been using TurboC++ because of my dumb school, we haven't even been taught about Templates so that's why I'm asking this, probably, dumb question.

Test.cpp

class Test_Class {
    int int_member;
    char char_member[20], group_ID;
    float float_member;

    static int currID;   //Static Var for Record Keeping in Files
public:
    Test_Class();
    void enter();
    void show_tablular();
    char get_group_id();
    static int ret_ID() { //Repeative calls are recorded & count is returned.
        return ++currID;
    }
};
int Test_Class::currID = 0;
void main() {
    clrscr();
    Test_Class Object, Object1;
    char file[20];
    strcpy(file,"lib_Test.dat");
    int run, records_read=0;
    char group_ID;
    cout<<"Enter the Group ID to be searched for: ";
    cin>>group_ID;
    run = view_recordT(Object, group_ID, file, &Test_Class::show_tablular, &Test_Class::get_group_id);
    if(run == false)
            cout<<"\nRecord not found!";
    cout<<"\n Total of "<<records_read<<" records have been read!";
    getch();
}

The goal here was to pass an Object to the view_recordT() and the addresses of the class methods from test.cpp so the view_recordT() can carry out the statements as if it was a member of Test_Class. But I get an error that:

get_compare_var() is not a member of Test_Class

Question


How can the view_recordT() function be modified so it runs completely independent of the Class that it'll be using to read the file?

1

There are 1 best solutions below

1
On BEST ANSWER

There are a lot of errors in that code (partly fault of turbo-c++) such as main having to return int and if(run = false) which should be if (run == false) and various non-standard functions.

After fixing most of them I can reproduce your error. Look up the syntax for pointer to member function. Yes the syntax is weird. In your case the correct syntax is

if((Object_1.*get_compare_char)() == Compare_char) {

After changing that line there are some more issues the compiler is warning about, such as records_read being uninitialized. Consider developing with a real compiler and then trying if turbo-c++ is able to compile it too.