namespace issue

Discussion in 'C++' started by Jason Heyes, Sep 16, 2005.

  1. Jason Heyes

    Jason Heyes Guest

    There are 3 files in the project. I compiled it with VC++ 6.0 and got an
    unresolved external symbol error. What am I doing wrong?

    // Foo.cpp
    #include "Foo.h"
    using namespace foo;

    void f() { }

    // Foo.h
    namespace foo { void f(); }

    // Main.cpp
    #include "Foo.h"

    int main()
    {
    foo::f();
    return 0;
    }

    --------------------Configuration: Examples - Win32
    Debug--------------------
    Compiling...
    ****** {BD Software Proxy CL v2.44a} STL Message Decryption is ON! ******
    Main.cpp
    Foo.cpp
    Linking...
    Main.obj : error LNK2001: unresolved external symbol "void __cdecl
    foo::f(void)" (?f@foo@@YAXXZ)
    Examples.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    Examples.exe - 2 error(s), 0 warning(s)

    Thanks guys.
    Jason Heyes, Sep 16, 2005
    #1
    1. Advertising

  2. Jason Heyes

    Srini Guest

    > // Foo.cpp
    > #include "Foo.h"
    > using namespace foo;
    >
    > void f() { }
    >
    > // Foo.h
    > namespace foo { void f(); }
    >
    > // Main.cpp
    > #include "Foo.h"
    >
    > int main()
    > {
    > foo::f();
    > return 0;
    >
    > }


    The problem is that there is no *definition* of 'void f()' function in
    the foo namespace. The definition you provided is a function in the
    *global* namespace. This how to correct this -

    // Foo.cpp
    #include "Foo.h"

    namespace foo {
    void f() { }
    }

    HTH
    - Srini
    Srini, Sep 16, 2005
    #2
    1. Advertising

  3. Jason Heyes wrote:
    > There are 3 files in the project. I compiled it with VC++ 6.0 and got an
    > unresolved external symbol error. What am I doing wrong?
    >


    Not putting f in the namespace foo.

    > // Foo.cpp
    > #include "Foo.h"
    > using namespace foo;
    >


    This tells the compiler to look at namespace foo when looking up names.
    It has nothing to with where a new name gets declared.

    > void f() { }


    This puts f in the global namespace.


    >
    > // Foo.h
    > namespace foo { void f(); }


    This declares f to be in the namespace foo, it has nothing to do with
    the f in foo.cpp which is a completely different function.

    >
    > // Main.cpp
    > #include "Foo.h"
    >
    > int main()
    > {
    > foo::f();
    > return 0;
    > }
    >


    The code you are looking for is

    // Foo.cpp
    #include "Foo.h"

    namespace foo
    {
    void f() {}
    }

    Perfectly logical, once you understand the difference between looking up
    a name and declaring a name. 'using ...' only affects the looking up of
    names.

    john
    John Harrison, Sep 16, 2005
    #3
  4. Jason Heyes

    Marcus Kwok Guest

    Srini <> wrote:
    > The problem is that there is no *definition* of 'void f()' function in
    > the foo namespace. The definition you provided is a function in the
    > *global* namespace. This how to correct this -
    >
    > // Foo.cpp
    > #include "Foo.h"
    >
    > namespace foo {
    > void f() { }
    > }


    Can't he also define it like this?

    void foo::f() { }

    --
    Marcus Kwok
    Marcus Kwok, Sep 16, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    563
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,126
  3. Anonymous
    Replies:
    3
    Views:
    529
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    448
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,352
    Howard
    Nov 29, 2004
Loading...

Share This Page