0

So I have a function pointer defined in my main class like this:

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);
         }
        }
    }
}

I Get the Following Error:

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

My ObjectiveFunctions source and header are defined like this:

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);
        };
    }
 }
}

Source:

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

Note that this whole thing work if I write the function inside the same file,but when I add the namespaces and seperate the Objectivefunctions into another Class I Get this Peoblem.

I Have looked alot in internet and they say you have to declare and define the functionsso the compiler won't get crazy. But I did declare and Define! I don't know what would be the problem.

1 Answer 1

2

You got your lines mixed up:

The full function name including the class name needs to go in the source file:

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;
          }
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

that won't work @nvoigt. Because in this case I have to get rid of static, which I don't want to.
@Afshinzkh nvoigt is saying you left out the class scope in your definition. That's why the linker cant find it. You don't have to get rid of the static qualifier.
Now I got it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.