【问题标题】:unresolved external symbol on static array of functions静态函数数组上未解析的外部符号
【发布时间】:2018-05-16 23:20:37
【问题描述】:

我在 Visual Studio 中遇到了无法解析的外部符号的问题。 我已经尝试了定义的所有组合,但我仍然收到消息

1>Exada.obj : error LNK2001: unresolved external symbol "public: static int (__cdecl** Exada::functions_array)(int)" (?functions_array@Exada@@2PAP6AHH@ZA)

我的头文件Exada.h中的声明是这样的

const int MAX_FUNCTIONS=179;
class Exada
{
public:
static int (*functions_array[MAX_FUNCTIONS + 2])(int);
…
};

Exada.cpp文件中的定义是

int (Exada:: *functions_array[MAX_FUNCTIONS + 2])(int) = { NULL,
&Exada::aporipteos_ar, //1
&Exada::aporipteos_ar, //2
&Exada::aporipteos_ar, //3
… Some address of functions 
}

感谢您的帮助。提前致谢。

【问题讨论】:

  • 请正确格式化您问题的代码块。
  • 如果可能,为什么不使用std::function。您的代码将更清晰,更易于阅读和调试。
  • 类中的数组声明不适用于指向成员的指针。所以我不明白你为什么期望它们匹配。
  • 我试过 static int (Exada::*functions_array[MAX_FUNCTIONS + 2])(int);但仍然有错误

标签: c++ visual-studio unresolved-external


【解决方案1】:

处理指向函数的指针数组可能很麻烦。使用中间类型别名声明:

class Exada
{
  // if functions are supposed to be normal or static member functions
  using t_Method = int ( * )(int);
  // if functions are supposed to be non-static member functions
  using t_Method = int ( Exada::* )(int);

  using t_Methods = t_Method[MAX_FUNCTIONS + 2];

  static t_Methods functions_array;
};

// cpp
Exada::t_Methods Exada::functions_array = { nullptr,

另外,最好使用::std::array 包装器而不是原始数组。

【讨论】:

    【解决方案2】:

    我已经尝试了定义的所有组合

    不是全部。而且由于您没有指定您想要什么样的定义,这主要是一个有根据的猜测。但是如果你碰巧想要一个指向非静态成员函数的静态成员数组,原始语法应该是这样的:

    const int MAX_FUNCTIONS=179;
    class Exada
    {
    public:
        static int (Exada::* functions_array[MAX_FUNCTIONS + 2])(int);
    //…
    };
    

    在你的实现文件中

    // One `Exada::` for pointer-to-member and one for scope
    int (Exada:: * Exada::functions_array[MAX_FUNCTIONS + 2])(int) = { NULL,
      &Exada::aporipteos_ar, //1
      &Exada::aporipteos_ar, //2
      &Exada::aporipteos_ar, //3
      //… Some address of functions 
    }
    

    这仍然很难读。所以我推荐一个类型别名来简化阅读和写作:

    const int MAX_FUNCTIONS=179;
    using member_type = int(int);
    class Exada
    {
    public:
        static member_type Exada::*functions_array[MAX_FUNCTIONS + 2];
    //…
    };
    

    【讨论】:

      猜你喜欢
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 2014-04-20
      • 2013-11-03
      • 2013-03-04
      相关资源
      最近更新 更多