C++ 函数指针问题

发布于 2024-07-18 09:50:20 字数 1420 浏览 6 评论 0原文

由于某种原因,尝试将此函数的指针传递给可变函数会产生以下错误:

1>c:\... : error C2664: 'PyArg_ParseTuple' : cannot convert parameter 3 from 'int (__cdecl *)(PyObject *,void *)' to '...'
1>        Context does not allow for disambiguation of overloaded function

PyArg_ParseTuple(args, "O&O&:CreateWindow",
    &Arg_Size2U<1>, &size,
    &Arg_String<2>, &title);

我不确定问题是什么,因为模板函数没有重载,并且模板参数是显式定义的,因此没有关于向哪个函数传递指针的问题...

是否有某种方法可以解决此问题,不会为每个参数转换函数创建大量版本,但如果出现错误,仍然可以在异常中包含参数编号? (更好的方法是将参数编号作为参数传递,因此我的 dll 中没有该函数的多个副本。)

编辑: 这似乎也会导致编译错误。 是否无法创建指向模板函数的函数指针,或者我是否需要以与普通函数不同的方式进行操作?

int (*sizeConv)(PyObject *,void *) = Arg_MakeSize2U<1>;
1>c:\... : error C2440: 'initializing' : cannot convert from 'int (__cdecl *)(PyObject *,void *)' to 'int (__cdecl *)(PyObject *,void *)'
1>        None of the functions with this name in scope match the target type

该函数声明为:

template<int ARG> int Arg_MakeSize2U(PyObject *obj, void *out)

EDIT2: 添加运算符的地址又给出了另一个不同的错误......

int (*sizeConv)(PyObject *,void *) = &Arg_MakeSize2U<1>;

1>c:\... : error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'int (__cdecl *)(PyObject *,void *)'
1>        None of the functions with this name in scope match the target type

For some reason trying to pass a pointer to this function to a varadic function produces the following error:

1>c:\... : error C2664: 'PyArg_ParseTuple' : cannot convert parameter 3 from 'int (__cdecl *)(PyObject *,void *)' to '...'
1>        Context does not allow for disambiguation of overloaded function

PyArg_ParseTuple(args, "O&O&:CreateWindow",
    &Arg_Size2U<1>, &size,
    &Arg_String<2>, &title);

I'm not sure what the problem is, since the template function has no over loads, and the template paremeters are defined explicitly, so theres no question over which function to pass a pointer for...

Is there some way around this that does not invlove create lots of versions for each argument conversion function, but can still have the argument number in the exception if there an error? (Better yet some way to pass the argument number as a paremeter, so there are not multiple copies of the function in my dll.)

EDIT:
This also seems to result in compile errors. Is there no way to create a function pointer to a template function, or do I need to do it diffrently from normal functions?

int (*sizeConv)(PyObject *,void *) = Arg_MakeSize2U<1>;
1>c:\... : error C2440: 'initializing' : cannot convert from 'int (__cdecl *)(PyObject *,void *)' to 'int (__cdecl *)(PyObject *,void *)'
1>        None of the functions with this name in scope match the target type

The function is declared as:

template<int ARG> int Arg_MakeSize2U(PyObject *obj, void *out)

EDIT2:
adding the address of operator gave yet another diffrent error...

int (*sizeConv)(PyObject *,void *) = &Arg_MakeSize2U<1>;

1>c:\... : error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'int (__cdecl *)(PyObject *,void *)'
1>        None of the functions with this name in scope match the target type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱我好吗 2024-07-25 09:50:20

这在 VC++ 2008 中编译得很好:

struct PyObject;

template<int ARG> int Arg_MakeSize2U(PyObject *obj, void *out)
{
    return 0;
}

int main()
{
    int (*sizeConv)(PyObject *,void *) = &Arg_MakeSize2U<1>;
    sizeConv(0, 0);
}

所以你必须做一些不同的事情。 也许你确实超载了,但你没有注意到。 发布更多上下文将有助于更好地诊断问题。

This compiles fine in VC++ 2008:

struct PyObject;

template<int ARG> int Arg_MakeSize2U(PyObject *obj, void *out)
{
    return 0;
}

int main()
{
    int (*sizeConv)(PyObject *,void *) = &Arg_MakeSize2U<1>;
    sizeConv(0, 0);
}

So you must be doing something differently. Maybe you do have an overload and you haven't noticed. Posting more context would help diagnose the problem better.

空‖城人不在 2024-07-25 09:50:20

从第二个错误来看,编译器似乎找不到实现。 您的模板函数定义是否在声明模板的头文件中?

From the second error it sounds like the compiler can't find the implementation. Is your template function definition in the header file where you declare your template?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文