Thread: Unresolved external symbol on used defined namespace

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    19

    Unhappy Unresolved external symbol on used defined namespace

    Hi all!

    I have a bit of a problem and Google isn't helping me

    I'm trying to implement a namespace, let's call it test.

    When i code my program like in the example below, it compiles, links and works fine.

    Main.cpp
    Code:
    namespace test
    {
    	void foo(void)
    	{
    		return;
    	}
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	test::foo();
    	return 0;
    }
    When i try to move the namespace to separate .h/.cpp files, like the ones below, the linking fails.

    Test.h
    Code:
    namespace test
    {
    	void foo(void);
    }
    Test.cpp
    Code:
    void test::foo(void)
    {
    	return;
    }
    Main.cpp
    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	test::foo();
    	return 0;
    }
    Compiler output
    1>Compiling...
    1>stdafx.cpp
    1>Compiling...
    1>Test.cpp
    1>Main.cpp
    1>Linking...
    1>Main.obj : error LNK2001: unresolved external symbol "void __cdecl test::foo(void)" (?foo@test@@YAXXZ)
    1>***\Visual Studio 2008\Projects\Test\Debug\Main.exe : fatal error LNK1120: 1 unresolved externals

    What am i doing wrong? Any help is greatly appreciated!

    Thanks!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Quasar View Post
    Test.cpp
    Code:
    void test::foo(void)
    {
    	return;
    }
    Should be:
    Code:
    namespace test
    {
    
        void foo(void)
        {
    	return;
        }
    
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I don't think that that is the problem: I recall that such qualification when defining the function is fine, and a quick test confirmed my memory.

    More likely Test.cpp simply was not compiled and/or linked.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    19
    Forget it, i'm an idiot...

    I just realized i was defining an inline function within the .cpp file and not the .h file. Moving the function to the correct place solved it.

    Oops...

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If it is an inline function in a header file it should most probably be:

    Code:
    namespace test
    {
        inline void foo(void)
        {
            return;
        }
    }
    Or you may get "multiple definitions" errors later on when more than one file includes it.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 8
    Last Post: 04-27-2006, 10:39 AM
  3. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  5. Compiler errors
    By BellosX in forum C Programming
    Replies: 2
    Last Post: 09-21-2001, 03:24 AM

Tags for this Thread