1

In myfunction.h, I declare the function myFunction() inside the namespace MyNamespace:

namespace MyNamespace{
  void myFunction();
}

In myfunction.cpp, I include the implementation:

#include "myfunction.h"

void MyNamespace::myFunction(){
   std::cout<<"My function"<<std::end;
}

Everything compiles fine.

If instead in the .cpp, I write:

using namespace MyNamespace;

void myFunction(){
   std::cout<<"My function"<<std::end;
}

I get an "Undefined Symbol" error during linking. Why?

If instead of a function, I declare a class in the header file, and the implementation of a method of that class in the .cpp file, I have no issue.

7
  • MyNamespace::myFunction() No return type. You sure this file's getting compiled? Commented Apr 17, 2021 at 4:01
  • Once I add the missing void I can't reproduce. Mind you, I wouldn't have been able to reproduce without the missing void. I'd get completely different errors. Commented Apr 17, 2021 at 4:03
  • Or would I? Maybe my compiler's a bit smarter or compiling to a different C++ Standard. Could you add the compiler command line to the question? Commented Apr 17, 2021 at 4:05
  • Sorry I forgot the void... Commented Apr 17, 2021 at 4:16
  • 2
    Does this answer your question? Linker error: undefined reference to function from header file (The gist of the accepted answer is that although you write the using namespace whatever, the functions you define won't actually get added to that namespace still) Commented Apr 17, 2021 at 4:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.