namespace issue

J

Jason Heyes

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.
 
S

Srini

// 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
 
J

John Harrison

Jason said:
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
 
M

Marcus Kwok

Srini said:
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() { }
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top