J
jakester
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_ostream<char,struct std::char_traits<char> > & __cdecl
graph:perator<<(class std::basic_ostream<char,struct
std::char_traits<char> > &,class graph::Node &)" (??6graph@@YAAAV?
$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@AAVNode@0@@Z)
referenced in function _main Program.obj
//Node.h
#ifndef NODE_H
#define NODE_H
#include <string>
#include <iostream>
using std:stream;
using std::string;
namespace graph {
class Node
{
friend ostream &operator<<(ostream &, 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(const string id) {
}
Node::~Node(void)
{
}
string Node::getId() const {
return id;
}
void Node::setId(const string &id) {
this->id = id;
}
}
ostream &operator<<(ostream &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_ostream<char,struct std::char_traits<char> > & __cdecl
graph:perator<<(class std::basic_ostream<char,struct
std::char_traits<char> > &,class graph::Node &)" (??6graph@@YAAAV?
$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@AAVNode@0@@Z)
referenced in function _main Program.obj
//Node.h
#ifndef NODE_H
#define NODE_H
#include <string>
#include <iostream>
using std:stream;
using std::string;
namespace graph {
class Node
{
friend ostream &operator<<(ostream &, 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(const string id) {
}
Node::~Node(void)
{
}
string Node::getId() const {
return id;
}
void Node::setId(const string &id) {
this->id = id;
}
}
ostream &operator<<(ostream &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;
}