未解析的重载函数类型 [英] unresolved overloaded function type
问题描述
我试图写一个模板类,它保存对它的模板参数类型的对象的引用,以及一个指向void的指针,返回该类的没有arg成员函数。但是,我在编译时遇到了关于'< unresolved function type>'
的错误。
I'm trying to write a templated class that holds a reference to an object of it's template parameter type, and a pointer to a void returning no arg member function of that class. However I'm getting an error about '<unresolved function type>'
when I compile.
template<class T>
class memberAction
{
public:
memberAction(void (T::*func)() , T& object);
private:
void (T::*m_func)();
T& m_object;
};
template<class T>
memberAction<T>::memberAction(void (T::*func)() , T& object)
{
m_func = func;
m_object = object;
}
class File
{
public:
File();
void TELUNUS_Open();
//memberAction<File>& getOpenAction();
private:
memberAction<File> m_OpenAction;
};
File::File():
m_OpenAction(TELUNUS_Open,*this)//Line with error on it
{
}
void File::Open()
{
//
}
使用g ++编译4.7.2我收到以下错误信息:
compiling with g++ 4.7.2 I get the following error message:
StackOverloadErrorExample.cpp|31|error: no matching function for call to 'memberAction<File>::memberAction(<unresolved overloaded function type>, File&)'|
看起来其他有类似编译器错误的人会把指向全局函数的指针与指向成员函数的指针混淆,但我特别声明构造函数采用指向其模板参数的成员函数的指针,并传递一个正确类型的成员函数。
It seems other people with a similar compiler error are confusing pointers to global functions with pointers to member functions, but I specifically declare the constructor as taking a pointer to a member function of it's template parameter, and pass it a member function of the correct type.
那么如何解决这个编译器错误?
So how do I resolve this compiler error?
推荐答案
我相信你需要传递& File :: TELUNUS_Open
-or & File :: Open
,无论哪个名称你实际上调用它 - 获取成员函数指针。 打开
具有函数键入 void(File ::)()
$ c>& File :: Open 有你真正想要的类型,函数指针类型 void(File :: *) / code>。除此之外,你将有
m_object
引用成员的问题。赋值运算符将尝试分配未初始化的引用:
I believe you need to pass &File::TELUNUS_Open
—or &File::Open
, whichever name you’ve actually called it—to get the member function pointer. Open
has the function type void (File::)()
whereas &File::Open
has the type you really want, the function pointer type void (File::*)()
. Beyond that, you’re going to have issues with the m_object
reference member. The assignment operator will try to assign to an uninitialised reference:
template<class T>
memberAction<T>::memberAction(void (T::*func)() , T& object)
{
m_func = func;
m_object = object;
}
您应该使用构造函数初始化程序列表:
You should use a constructor initializer list instead:
template<class T>
memberAction<T>::memberAction(void (T::*func)() , T& object)
: m_func(func),
m_object(object) {}
这篇关于未解析的重载函数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!