473,698 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

namespaces, linkage error, operator overloading

I am using Visual C++ 2007 to build the code below. I keep getting
linkage error. Could someone please tell me what I am doing wrong? The
code works until I start using namespace for my objects.

Error 1 error LNK2019: unresolved external symbol "class
std::basic_ostr eam<char,struct std::char_trait s<char & __cdecl
graph::operator <<(class std::basic_ostr eam<char,struct
std::char_trait s<char &,class graph::Node &)" (??6graph@@YAAA V?
$basic_ostream@ DU?$char_traits @D@std@@@std@@A AV12@AAVNode@0@ @Z)
referenced in function _main Program.obj

//Node.h
#ifndef NODE_H
#define NODE_H

#include <string>
#include <iostream>

using std::ostream;
using std::string;

namespace graph {
class Node
{
friend ostream &operator<<(ost ream &, Node &);
public:
Node(void);
Node(const string id);
~Node(void);

string getId() const;
void setId(const string &);
private:
string id;
};
}

#endif

//Node.cpp
#include "Node.h"

using graph::Node;

namespace graph {
Node::Node(void )
{
}
Node::Node(cons t string id) {
}

Node::~Node(voi d)
{
}

string Node::getId() const {
return id;
}

void Node::setId(con st string &id) {
this->id = id;
}
}

ostream &operator<<(ost ream &output, Node &node) {
output << node.getId();
return output;
}

//Program.cpp
#include "Node.h"

using graph::Node;
using std::cout;
using std::endl;

int main() {
Node a("a");
Node b("b");

cout << a << " and " << b << endl;
}

Apr 14 '07
11 3810
On Apr 14, 8:05 pm, "James Kanze" <james.ka...@gm ail.comwrote:
On Apr 14, 11:14 pm, "twomers" <twom...@gmail. comwrote:

[...]
namespace graph {

[...]
ostream &operator<<(ost ream &output, Node &node) {

If this declaration compiles, it's time to change compilers,
fast. It's not, and never has been, legal C++; a member
operator<< takes only one (explicit) parameter.
output << node.getId();
return output;
}
}

--
James Kanze (Gabi Software) email: james.ka...@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
In my reference book, they say something similar to what you are
saying too (overloading operators as member function should not have 2
operands). I am quite confused. But the code does compile with VC++
and g++. This observation adds much to my confusion.

Apr 16 '07 #11
jakester wrote:
On Apr 14, 8:05 pm, "James Kanze" <james.ka...@gm ail.comwrote:
On Apr 14, 11:14 pm, "twomers" <twom...@gmail. comwrote:
[...]
namespace graph {
This is what I misread.
[...]
ostream &operator<<(ost ream &output, Node &node) {
If this declaration compiles, it's time to change compilers,
fast. It's not, and never has been, legal C++; a member
operator<< takes only one (explicit) parameter.
output << node.getId();
return output;
}
}
In my reference book, they say something similar to what you are
saying too (overloading operators as member function should not have 2
operands). I am quite confused. But the code does compile with VC++
and g++. This observation adds much to my confusion.
The problem is simply that I misread the code, and took a
namespace for a class. Overloaded binary operators take one
operand if a member, two if a free function. In namespace
scope, they are free functions, so two operands are in order.

I'm afraid my posting just added to the confusion. Namespaces
and classes have nothing to do with one another.

With regards to your original code: a friend declaration does
*not* declare a member, but rather a function in the enclosing
namespace. Which means, of course, that you have to define the
function in the enclosing namespace; otherwise, you're defining
some other function.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 16 '07 #12

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

Similar topics

12
1995
by: Santiago de Compostela | last post by:
Hi The following program doesn't compile on MS VC++ or Bloodshed Dev-C++ #include <iostream> int strlen(const char *in) {
2
2249
by: glen stark | last post by:
Hi. I had an interesting experience with a brand x compiler recently. I had class defined within a namespace, and in that class I declared the "<<" operator as a friend to that class. Then, in the cpp code I implemented the operator overloading within a "using namespace" context, only to get access errers. To get it to work I had to put a namespaceName:: in front of the operator keyword... for your edification, here is a sample...
4
2451
by: Tron Thomas | last post by:
I have read that anonymous namespaces are preferred in C++ over declaring global statics because the namespace can hide information from other translation units and at the same time provide external linkage allowing members of the namespace to be used as template arguments. I'm not quite sure I'm clear on what this really means. What is an example of this anoymous namespace benefit over the use of global statics?
12
2970
by: RA Scheltema | last post by:
Hi all, I have the following code: namespace A { inline void func(int) { ...; } inline void func(float) { ...; } inline void func(char) { ...; } }
22
6005
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be given extern "C" linkage where it is to be assigned to a C function pointer. Ian
10
6180
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
2
1864
by: Beeeeeeeeeeeeves | last post by:
Does C# really HAVE to have namespaces? Isn't there any way I can turn them off? I did a bit of dabbling in VB.NET but have decided not to switch to it permanantly as I've got too much pre-written generic code in C# and the cast operator overloading thing but one of the things I like about VB.NET is it doesn't enthrust the pointless idea of namespaces on you, even though I notice that you can have them if you want. Are they turn-off-able?
8
2589
by: andrew browning | last post by:
I am getting an istream overloading error that looks like this: error: no match for 'operator>>' in 'ins >> target->abrowning_rational::Rational::numerator' Below is the .h file: #ifndef RATIONAL #define RATIONAL #include <iostream>
1
4242
by: thetrueaplus | last post by:
Hey all. I posted this originally: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/b1e4b76201ce17f1 I got the enums to work how I like but I am having a problem with the overloading of the assignment operator: namespace UserAffilliationType {
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9026
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8893
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7723
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.