将函数指针传递给模板 class
Passing a function pointer in to templated class
我的问题很简单...我有一个模板化的二叉搜索树。我需要能够在调用构造函数时让用户传入比较函数。我的代码一直对我大喊大叫,直到我也模板化了用户定义的函数(在驱动程序中)。这打破了我对模板如何工作的直觉。这让我想知道我的代码是否没有按照我的预期进行模板化。我很好奇在声明模板化的 class 对象时让用户模板化它们的函数是否正常(特别是当该对象需要传入用户定义的函数时)。如果这不正常,那么我知道我的代码有问题。
这是我之前遇到的错误。那些 "undeclared identifiers" 只是第 93 行一个错误的结果。这是我尝试创建 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;
};
我认为你的问题在于你对 myTree 的初始化。我写了一些我认为模仿您的用例的代码。我相信特别是最后一行将是您问题的解决方案:
//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);
希望这对您有所帮助。如果我误解了您的问题,请告诉我。
我认为部分问题在于您的参数是整数,而它们似乎应该是 T,以匹配用户定义的类型。假设它们是 int 可以很好地进行测试,但如果需要任何其他数据类型则不会成立。如果是这种情况,那么对其进行模板化是有意义的,因为该函数正在有效地转置到您的头文件中,该文件似乎是模板化的。当然,我对此比较陌生,所以如果我犯了逻辑错误,请告诉我!
我的问题很简单...我有一个模板化的二叉搜索树。我需要能够在调用构造函数时让用户传入比较函数。我的代码一直对我大喊大叫,直到我也模板化了用户定义的函数(在驱动程序中)。这打破了我对模板如何工作的直觉。这让我想知道我的代码是否没有按照我的预期进行模板化。我很好奇在声明模板化的 class 对象时让用户模板化它们的函数是否正常(特别是当该对象需要传入用户定义的函数时)。如果这不正常,那么我知道我的代码有问题。
这是我之前遇到的错误。那些 "undeclared identifiers" 只是第 93 行一个错误的结果。这是我尝试创建 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;
};
我认为你的问题在于你对 myTree 的初始化。我写了一些我认为模仿您的用例的代码。我相信特别是最后一行将是您问题的解决方案:
//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);
希望这对您有所帮助。如果我误解了您的问题,请告诉我。
我认为部分问题在于您的参数是整数,而它们似乎应该是 T,以匹配用户定义的类型。假设它们是 int 可以很好地进行测试,但如果需要任何其他数据类型则不会成立。如果是这种情况,那么对其进行模板化是有意义的,因为该函数正在有效地转置到您的头文件中,该文件似乎是模板化的。当然,我对此比较陌生,所以如果我犯了逻辑错误,请告诉我!