在命名空间中调用函数指针

Calling a function pointer within namespaces

所以我在主 class 中定义了一个函数指针,如下所示:

namespace Lab
{
    namespace Math
    {
        namespace Port
        {
         void main()
         {
          typedef void(*objfunptr)(int, int, const double[], double[], double[], int);
          objfunptr objfun= ObjectiveFunctions::ExampleObjFun;
          // Till Here it compiles fine but after calling the function we have error
          objfun(m, n, xPtr, fPtr, fjacPtr, td);
         }
        }
    }
}

我收到以下错误:

Error   16  error LNK2001: unresolved external symbol "public: static void __cdecl Lab::Math::Port::ObjectiveFunctions::ExampleObjFun ...

我的 ObjectiveFunctions 源和 header 定义如下:

Header:

namespace Lab
{
    namespace Math
    {
        namespace Port
        {
        public class ObjectiveFunctions
        {
        public:
            static void ObjectiveFunctions::ExampleObjFun(int m, int n, const double x[], double f[], double fjac[], int fjacp);
        };
    }
 }
}

来源:

namespace Lab
{
     namespace Math
    {
        namespace Port
        {
          static void ExampleObjFun(int m, int n, const double x[], double f[], double fjac[], int fjacp){
         //Do whatever;
          }
        }
    }
}

请注意,如果我在同一个文件中编写函数,那么整个事情都有效,但是当我添加名称空间并将目标函数分离到另一个文件中时 Class 我得到了这个 Peoblem。

我在网上看了很多,他们说你必须声明和定义函数,这样编译器才不会发疯。但是我确实声明并定义了!我不知道会是什么问题。

你把台词弄错了:

包含 class 名称的完整函数名称需要放在源文件中:

namespace Lab
{
     namespace Math
    {
        namespace Port
        {
                       // V V V  
          static void ObjectiveFunctions::ExampleObjFun(int m, int n, const double x[], double f[], double fjac[], int fjacp){
         //Do whatever;
          }
        }
    }
}