简体   繁体   中英

Passing a function pointer in to templated class

My question is fairly simple... I have a binary search tree that is templated. I need to be able to have to user pass in a compare function when a constructor is called. My code kept on yelling at me until I templated the user defined function as well (in the driver program). This broke my intuition as to how templating works. It makes me wonder if my code is not templated as I expect. I'm just curious if it is normal to have a user template their functions when declaring a class object that is templated (specially when that object requires a user defined function to be passed in). If this is not normal then I know i have something incorrect with my code. 在此处输入图像描述

This is the error I was getting before. Those "undeclared identifiers" are just are just a result of the one error on line 93. This is where I am trying to create an instance of a class.

//Part of driver program. 
//Not sure why code doesn't work without template <typename T> 

template <typename T>
int compare(const int data, const int nodeData) 
//User defined compare function. Takes two values and compares them and returns a -1, 0, or 1 if it is less than equal to or greater than respectively. 
{
    int returnValue; //The value that will be returned. 
    if (data < nodeData)
    {
        returnValue = -1;
    }
    else if (data > nodeData)
    {
        returnValue = 1;
    }
    else
    {
        returnValue = 0;
    }
    return(returnValue);
}
//Now for the code that is inside my class. 
//The following is my decoration for the function pointer within my class.
//////////////
int (*funcCompare)(T i, T j); 
////////////////

//And lastly here is my constructor for my class 
    SplayTree(int(*compFunction)(const T, const T)) //Constructor that takes a pointer to a comparison function as an arugment. 
    {
        funcCompare = compFunction;
    };

I think your issue lies in your initialization of myTree. I wrote some code that I think mimics your use-case. I believe the last line in particular will be the solution to your problem:

    //header file
    template <typename T>
    class TemplatedClass {
    public:
        TemplatedClass(int(*compFunction)(const T, const T)) :
            funcCompare(compFunction)
        {}
    private:
        int (*funcCompare)(const T i, const T j);
    };
    /////////////////////////////////////////////////////////////
    //compare function
    int compare(const int data, const int nodeData)
    {
        int returnValue; 
        if (data < nodeData)
        {
            returnValue = -1;
        }
        else if (data > nodeData)
        {
            returnValue = 1;
        }
        else
        {
            returnValue = 0;
        }
        return(returnValue);
    }
    //////////////////////////////////////////////////////////////
    //initialization
    TemplatedClass<int> tc(compare);

Hope this helps. Please let me know if I misunderstood something about your question.

I believe part of the issue is that your arguments are ints, when it seems that they should be T, to match the user-defined type. Assuming they will be int is fine for testing, but will not hold up if any other data type is desired. If that is the case, it makes sense for it to be templated, as the function is effectively being transposed into your header file, which appears to be templated. Granted, I'm relatively new to this, so if I made a logical mistake please let me know!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM