anonymous namespace issue

J

Jason Heyes

I wrote:

namespace { void f(); }
void f() { std::cout << "hello world" << std::endl; }

When I compile, I receive this error:

unresolved external symbol "bool __cdecl `anonymous namespace'::f(void)"

What have I done wrong? Thanks.
 
G

Geo

Jason said:
I wrote:

namespace { void f(); }
void f() { std::cout << "hello world" << std::endl; }

When I compile, I receive this error:

unresolved external symbol "bool __cdecl `anonymous namespace'::f(void)"

What have I done wrong? Thanks.

Exactly what it say, you've not supplied a definition for

bool __cdecl `anonymous namespace'::f(void)
 
F

Fraser Ross

"Geo"
Exactly what it say, you've not supplied a definition for

bool __cdecl `anonymous namespace'::f(void)

Presumably f() is called, and the global f() is in a different
translation unit or there would be ambiguity with the function call
resolution.

Fraser.
 
G

Greg

Jason said:
I wrote:

namespace { void f(); }
void f() { std::cout << "hello world" << std::endl; }

When I compile, I receive this error:

unresolved external symbol "bool __cdecl `anonymous namespace'::f(void)"

What have I done wrong? Thanks.

The definition for f() must be inside the anonymous namespace. After
all, the namespace has no name, so there's no way to qualify f() to
place it in the anonymous namespace.

Greg
 
J

Jason Heyes

Geo said:
Exactly what it say, you've not supplied a definition for

bool __cdecl `anonymous namespace'::f(void)

How do I supply one? Do I need to write this?

namespace
{
void f() { std::cout << "hello world" << std::endl; }
}

If I don't want to define f in the block of the anonymous namespace, where
can I define it and how? I tried this but it won't compile.

namespace { void f(); }
void ::f() { std::cout << "hello world" << std::endl; }

NB: I use VC++ 6.0.
 
J

Jason Heyes

Greg said:
The definition for f() must be inside the anonymous namespace. After
all, the namespace has no name, so there's no way to qualify f() to
place it in the anonymous namespace.

Thank you for your answer.

I'm curious. Why does this code compile successfully if what you say is
true?

namespace n
{
namespace
{
void f();
}
}

void n::f() { }

int main()
{
n::f();
return 0;
}
 
R

Rolf Magnus

Jason said:
If I don't want to define f in the block of the anonymous namespace, where
can I define it and how?

Why do you want to define it outside the anonymous namespace if you declared
it inside?
 
J

Jonathan Mcdougall

Jason said:
Thank you for your answer.

I'm curious. Why does this code compile successfully if what you say is
true?

namespace n
{
namespace
{
void f();
}
}

void n::f() { }

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

It should not. n::f() does not exists. This example is functionally
equivalent to:

namespace n
{
namespace m
{
void f();
}

using namespace m;
}

void n::f()
{
}

which is clearly illegal. Your compiler is broken.


Jonathan
 
G

Greg

Jason said:
Thank you for your answer.

I'm curious. Why does this code compile successfully if what you say is
true?

namespace n
{
namespace
{
void f();
}
}

void n::f() { }

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

This program should generate this error:

test.cc:9: error: 'void n::f()' should have been declared inside 'n'

Even without the error, n::f() refers to an f() inside namespace n and
not within an inner, anonymous namespace.

Greg
 

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