473,385 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Unnamed namespace predicament...


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
Apr 8 '06 #1
3 2579

Tomás wrote:
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 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


Apr 8 '06 #2

Rani wrote:
Tomás wrote:

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
void Ape(); //Function Declaration

Ape();
}

namespace {

void Ape()
{
;
}

}


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?

Apr 8 '06 #3
In article <uO******************@news.indigo.ie>,
"Tomás" <NU**@NULL.NULL> wrote:
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();
//...
}
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anthony | last post by:
Hello, I have been reading up on unnamed namespaces in the context of hiding classes as mentionned by the GOF Facade pattern description. I was hoping someone could shed some light on this. I...
1
by: marco_segurini | last post by:
Hi, I like to know if this code is legal: namespace { void unnamed_ns(){} } class test
1
by: Marco Jez | last post by:
What are the requirements imposed by the Standard on unnamed namespaces? I mean, is the hidden name guaranteed to be unique across multiple translation units, or only within the declaring...
3
by: Sandy | last post by:
Hi, I have two files as folllows file1.cpp #include<iostream> using namespace std; namespace { void show(); void fun() { cout<<"fun called\n"; } }
2
by: dioscuroi | last post by:
Is there any difference between declaring in unnamed namespace and i global namespace? I can't recognize this p.s: Have a nice day ^_________ -
9
by: Ivan Mascovich | last post by:
Previous posts (and compilers) confirm that class X { friend void Y() ; } ; does not match namespace
9
by: Tom Plunket | last post by:
The project I'm currently on makes heavy use of unnamed structures to represent an "object hierarchy" of a sort. E.g. struct BaseObject { int aMember; int anotherMember; // etc.
1
by: vsgdp | last post by:
I just want to clarify: Say I have a helper class that is only used in one translation file (e.g., a predicate class). I don't want the name of this class to conflict with other class names in...
3
by: CrazyJohn | last post by:
Hi guys, This is my first time posting question here, if I break any rules, please kindly point out. And I'm really glad to be a part of this community. Here is my question, Our lecturer...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.