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;
}
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;
}
Comment