【问题标题】:C++ Invalid initialization error with function pointers带有函数指针的 C++ 无效初始化错误
【发布时间】:2013-03-27 09:21:47
【问题描述】:

我正在使用指向模板函数的指针,这些函数具有数组引用 参数,例如:

template< size_t N >
double MyFunction( double (&x)[N] );

我将使用以前的函数类型,就像另一个函数中的参数一样

template< size_t N >
bouble BigFunction( double (* PtrFunc)( double (&)[N] ), ... ) {
    double x[3];
    ...
    // fill x
    // call PtrFunc
    double y = (* PtrFunc)( x ); // error: ...
    ...
}

在主程序中,我调用double z = BigFunction( &amp;MyFunction, ... ),但是当我尝试使用 GCC 4.7.2 进行编译时,对于 N = 3 的数组的特定情况,我收到以下错误(该情况并不重要,仅解释了 [3]在错误中)

错误: 'double [(((sizetype))(((ssizetype)dim[1]) + -1))+ 1)]'

如何解决仍然使用数组引用的问题?

注意.- 如果我用 typedef 或 std::function 的结构替换指针参数,问题是一样的。

这里有一个完整的例子ideone

【问题讨论】:

  • 基于编译器错误,我认为这个案例实际上 is 很重要。你能展示你如何调用BigFunction(),以及你在调用中使用的所有参数的声明吗?
  • 我像BigFunction( &amp;MyFunction )BigFunction内部一样调用函数我有一个数组double x[3],我调用(* PtrFunc)( x ),错误出现在这一行跨度>
  • @PedroGuarderas Just BigFunction(&amp;MyFunction) 不应该编译,因为在这种情况下编译器不可能推断出N。您可以发布minimal, complete example 重现错误吗?例如,我们可以粘贴到 ideone 中。
  • 那个著名的内置类型bouble

标签: c++ templates function-pointers


【解决方案1】:

在您的示例中,http://ideone.com/oWef7L 将第 24 行从

double x[M];

double x[N];

这编译没有错误:http://ideone.com/VouWlG

【讨论】:

    猜你喜欢
    • 2012-03-31
    • 1970-01-01
    • 2019-09-02
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    相关资源
    最近更新 更多