473,404 Members | 2,213 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,404 software developers and data experts.

unnamed namespace problem

Hi,
I have two files as folllows

file1.cpp
#include<iostream>
using namespace std;
namespace {
void show();
void fun() { cout<<"fun called\n"; }
}

int main()
{
show();
return 0;
}
file2.cpp
#include<iostream>
using namespace std;
namespace{
void fun();
void show(){
fun();
cout<<"show called\n";
}
}

While trying to run this the linker is giving following message
Undefined first referenced
symbol in file
(anonymous namespace)::fun() file2.o
(anonymous namespace)::show() file1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
As far as i think, it should be able to find the definitions because
everything here belongs to a single "un-named" namespace.
Then why am i getting the problem?
Sep 24 '05 #1
3 2331

Sandy wrote:
Hi,
I have two files as folllows

file1.cpp
#include<iostream>
using namespace std;
namespace {
void show();
void fun() { cout<<"fun called\n"; }
}

int main()
{
show();
return 0;
}
file2.cpp
#include<iostream>
using namespace std;
namespace{
void fun();
void show(){
fun();
cout<<"show called\n";
}
}

While trying to run this the linker is giving following message
Undefined first referenced
symbol in file
(anonymous namespace)::fun() file2.o
(anonymous namespace)::show() file1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
As far as i think, it should be able to find the definitions because
everything here belongs to a single "un-named" namespace.
Then why am i getting the problem?


Every unnamed namespace is unique - which means that every unnamed
namespace is a different namespace from every other unnamed namespace.
Not only across files and even across time. An unnamed namespace may
not even be the same unnamed namespace that it was before it was last
compiled.

In this case there are two show() functions and two fun() functions
declared, but fewer than four functions are actually defined.

Greg

Sep 24 '05 #2
Greg wrote:
Every unnamed namespace is unique - which means that every unnamed
namespace is a different namespace from every other unnamed namespace.
Not only across files and even across time. An unnamed namespace may
not even be the same unnamed namespace that it was before it was last
compiled.


That's NOT true.

All the unnamed namespaces in a single translation unit are the same
namespace. I'm not even sure what the point you're trying to make
about time is. Multiple unnamed namespaces in the same file are hence
the same namespace.

The namespace is however different from other namespaces in other
translation units (which is what the user has).
Sep 24 '05 #3
"Sandy" wrote:
Hi,
Hello.
I have two files as folllows

file1.cpp
#include<iostream>
using namespace std;
Ewwwwwwwww. That dumps thousands of names from namespace "std"
into the global namespace. Bad idea. (Creates danger of name
collision.) I wish textbook authors would stop telling people
to do that.
namespace {
void show();
void fun() { cout<<"fun called\n"; }
}

int main()
{
show();
return 0;
}

And the name of this namespace is??????????????
If you want to be able to access stuff in that
namespace from other files, you need to name it.
Try this instead:

// file1.cpp
#include<iostream>
using std::cout;
namespace MyNiftyNameSpace
{
void show();
void fun() {cout << "fun called\n";}
}

int main()
{
MyNiftyNameSpace::show();
return 0;
}

file2.cpp
#include<iostream>
using namespace std;
namespace{
void fun();
void show(){
fun();
cout<<"show called\n";
}
}
Once you've named a namespace, you can add to it in other
files, like this:

// file2.cpp
#include<iostream>
using std::cout;
namespace MyNiftyNameSpace
{
void fun();
void show(){
fun();
cout<<"show called\n";
}
}
While trying to run this the linker is giving following message
Undefined first referenced
symbol in file
(anonymous namespace)::fun() file2.o
(anonymous namespace)::show() file1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
As far as i think, it should be able to find the definitions because
everything here belongs to a single "un-named" namespace.
Then why am i getting the problem?


You were using MULTIPLE un-named namespaces (one per file).
Interection equals null set. (Ie, the namespaces are disjoint.)

But if you name a namespace, you can add stuff to in in many files,
and the linker will hunt-down the missing pieces and link them
together for you.
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
Sep 25 '05 #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...
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
3
by: Tomás | last post by:
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...
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.