473,387 Members | 1,362 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,387 software developers and data experts.

I finally figured it out! namespaces and the ODR

A while back I posted some code along with an error message I got when I
tried to compile it. I thought at one point I had solved all the problems.
But then I tried it again, and if failed. The code I posted here didn't
have namespaces in it. I had originally tried to compile the same code
with namespaces. The one subtlety I missed was that overloaded operator
definitions are namespace local. If I declare this in my header file:

//BaseNode.hh
namespace sth{
ostream& operator<<(ostream& out, const BaseNode& bn);
}

I have to qualify the operator<<(...); like this:
//BaseNode.cc

ostream& sth::operator<<(ostream& out, const BaseNode& bn)
{
return bn.print(out);
}

Note the sth:: before the operator<<(..) I had not been doing that. That
caused the compile to fail. I then put the definition back in the header
file, and that violated the ODR. Put all the code in one file, and it all
worked fine.

This is the original message I'm referring to:
news://Message-ID: <_c********************@speakeasy.net>
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #1
1 1539
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message news:<vp********************@speakeasy.net>...

[snip]
//BaseNode.hh
namespace sth{
ostream& operator<<(ostream& out, const BaseNode& bn);
} I have to qualify the operator<<(...); like this:
//BaseNode.cc ostream& sth::operator<<(ostream& out, const BaseNode& bn)
{
return bn.print(out);
}


Alternately, you can define operator<< in BaseNode.cc like this:

namespace sth {

std::ostream& operator<<(std::ostream& stream,
const BaseNode& node)
{
// implementation
}

} // close namespace sth

It is considered bad practice by many to state 'using namespace std;'.
Therefore, you should always use fully qualified names. Note that it
is *not* a good idea to write BaseNode.cc like this (for example):

using namespace sth;

void freeFunction(void) { /*...*/ }

Since 'freeFunction' might clash with a symbol from another namespace
dragged into BaseNode.cc from another header file.

HTH, /david
Jul 22 '05 #2

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

Similar topics

3
by: Roger Redford | last post by:
Dear experts, I'm trying to learn java on my own. I picked up a sample online, but it is not compiling right: ------------------------------------------------ import java.io.*;
4
by: Brian Alexander | last post by:
Hello; I'm curious to know how people preserve exceptions that arise in a try ... finally block. Consider this example: try: getResource() doSomething() finally: alwaysFreeResource()
16
by: Ken | last post by:
What is the purpose of a finally block in try-catch- finally? I am confused because, if I am reading the definition correctly, this block seems unnecessary. Consider the following from the Visual...
8
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
9
by: Andy Turner | last post by:
I was just pondering about what the Finally in an exception actually does. Well, I realise that it always gets called regardless of whether the exception is thrown or not, but isn't that the same...
7
by: Sean Kirkpatrick | last post by:
I got caught with my pants down the other day when trying to explain Try...Catch...Finally and things didn't work as I had assumed. Perhaps someone can explain to me the purpose of Finally. I've...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
12
by: Carl J. Van Arsdall | last post by:
Hey python people, I'm interested in using the try/finally clause to ensure graceful cleanup regardless of how a block of code exits. However, I still am interested in capturing the exception....
21
by: Mark Rae | last post by:
Hi, I have an ASP.NET 2 web application (not web site) project and am using a web deployment project to deploy to the testing, then the production server. All has been working well for months...
12
by: David Lozzi | last post by:
Howdy, I ran into a very interesting issue and I'm curios as to how this is suppose to work. I am using Try...Catch...Finally statements for all database connectivity in my ASP.NET 2.0 web...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.