Unnamed namespace predicament...

T

Tomás

I have a header file/source file combo.

There's approximately five functions in the source file. Only one of the
functions should be used by other translation units, so I've defined the
other four functions within an unnamed namespace. The header file only
contains a declaration for the one function.

My function needs to call the other four functions. My code won't compile,
giving the following error:

c:\>g++ code.cpp -ansi -pedantic -o program.exe
C:\Temp/ccUvbaaa.o(.text+0x7):code.cpp: undefined reference to `Ape()'

Here's the sample code file which demonstrates the problem:

void Monkey()
{
void Ape(); //Function Declaration

Ape();
}

namespace {

void Ape()
{
;
}

}


int main()
{
Monkey();
}



If I switch the order around, as follows:



namespace {

void Ape()
{
;
}
}

void Monkey()
{
Ape();
}

int main()
{
Monkey();
}


Then my code compiles without error.

In order for me to be able to put "Monkey" first, then I need to declare
"Ape" before I use it. So how do I declare "Ape"? I've tried declaring it
both inside the function "Monkey" and outside the function "Monkey", but
neither will work. Also I've tried:

void ::Ape();

but that doesn't work either. I've also tried putting "extern" before it,
but to no avail.


-Tomás
 
R

Rani

Tomás said:
I have a header file/source file combo.

There's approximately five functions in the source file. Only one of the
functions should be used by other translation units, so I've defined the
other four functions within an unnamed namespace. The header file only
contains a declaration for the one function.

My function needs to call the other four functions. My code won't compile,
giving the following error:

c:\>g++ code.cpp -ansi -pedantic -o program.exe
C:\Temp/ccUvbaaa.o(.text+0x7):code.cpp: undefined reference to `Ape()'

Here's the sample code file which demonstrates the problem:
Put this new declaration here -
namespace { void Ape(); }
void Monkey()
{
Comment out the follg declaration
 
N

Noah Roberts

Rani said:
Put this new declaration here -
namespace { void Ape(); }
Comment out the follg declaration

Well doesn't that sort of nullify the whole reason for unnamed
namespaces? What if you really want to make that type only visible to
your class or function and those that use it no?

I had a similar problem and I had to not use the namespace to solve it,
but I would have liked finding another way:

X.h...

class X
{
friend class Y;
public:
Z * getZ() const;
};


X.cpp....

#include "Z.h"
#include "X.h"

namespace
{
class Y : public Z { ... };
};

Z * X::getZ() { return new Y(); }

If Y is in an anonymous namespace that code won't work, but if it isn't
then it does. Not that big of a deal but it makes me wonder how you
address things like this.

If you declared a namespace { class Y; } before the class definition of
X then now everyone that includes X.h knows about Y, no?
 
D

Daniel T.

"Tomás said:
I have a header file/source file combo.

There's approximately five functions in the source file. Only one of the
functions should be used by other translation units, so I've defined the
other four functions within an unnamed namespace. The header file only
contains a declaration for the one function.


Do it this way:

// header file

void Moneky();

// source file

namespace {
void Ape() {
//...
}
} // namespace

void Monkey() {
//...
Ape();
//...
}
 

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,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top