2

So for some reason I have experienced the behavior that adding a namespace to my .h and .cpp files for a set of functions breaks my linker. I'm using Visual Studio 2012. Here's my scenario (simplified)

functions.h

int functionA();
int functionB();

functions.cpp

#include "functions.h"

int functionA() { return 0; }//we could pretend there's actual code here
int functionB() { return 0; }//we could pretend there's actual code here

and the actual useage of it is in some cpp file thusly:

pointers.h

#include "functions.h"

class GetPointers
{
public:
    typedef int (*FunctionPointer)(void);

    static FunctionPointer funcPointerA() { return &functionA; }
    static FunctionPointer funcPointerB() { return &functionB; }
};

Well that's all fine and dandy. I can call the static method of GetPointers and get a function pointer which works. Everythings been tested and everything is happy. Now I thought I would simply add some namespaces to make sure I don't have any problems again in the future. So I simply modify the three code files to use namespaces. What happens is a link error which refers to the function funcPointerA() and funcPointerB() of the GetPointers class, with the full namespace name to functionA and functionB.

functions.h

namespace fun {

int functionA();
int functionB();

}

functions.cpp

#include "functions.h"

using namespace fun;

int functionA() { return 0; }//we could pretend there's actual code here
int functionB() { return 0; }//we could pretend there's actual code here

and the actual useage of it is in some cpp file thusly:

pointers.h

#include "functions.h"

namespace fun {

class GetPointers
{
public:
    typedef int (*FunctionPointer)(void);

    static FunctionPointer funcPointerA() { return &functionA; }
    static FunctionPointer funcPointerB() { return &functionB; }
};

}

I don't get a build error, only a link error about fun::functionA and fun::functionB. Is there something implicitly wrong with using function pointers from namespaces?

1
  • 1
    using namespace fun; only lets you use functions from the fun namespace. It does not make functionA and functionB part of that namepsace. You have in effect redeclared those functions in the global scope for that file. Commented Jun 30, 2014 at 13:28

1 Answer 1

4

The problem is that your definitions:

int functionA() { return 0; }
int functionB() { return 0; }

are in the global namespace; so they declare new functions there rather than define the functions declared in namespace fun.

The best fix is to qualify the names in the definitions:

int fun::functionA() { return 0; }
int fun::functionB() { return 0; }

This is preferable to putting the definitions inside the namespace, since it gives a compile-time check that the functions match their declarations.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick an concise answer.

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.