名称空间,链接错误,运算符重载 [英] namespaces, linkage error, operator overloading

查看:99
本文介绍了名称空间,链接错误,运算符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual C ++ 2007来构建下面的代码。我一直收到

连锁错误。有人可以告诉我我做错了什么吗?

代码一直有效,直到我开始为我的对象使用命名空间。


错误1错误LNK2019:未解析的外部符号类

std :: basic_ostream< char,struct std :: char_traits< char& __cdecl

graph :: operator<<(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)

在函数_main Program.obj中引用


//Node.h

#ifndef NODE_H

#define NODE_H


#include< string>

#include< iostream>


使用std :: ostream;

使用std :: string;


命名空间图{

class Node

{

friend ostream& operator<<(ostream&,Node&);

public:

Node(void);

节点(const string id);

~Node(void);


string getId()const;

void setId (const string&);

private:

string id;

};

}


#endif


//Node.cpp

#include" Node.h"


使用graph :: Node;


命名空间图{

Node :: Node(void)

{

}

Node :: Node(const string id){

}


节点::〜节点(无效)

{

}


字符串Node :: getId()const {

返回id;

}


void Node :: setId(const string& id){

this-> id = id;

}

}


ostream& operator<<(ostream& output,Node& node){

输出< ;< node.getId();

返回输出;

}


//Program.cpp

#include" Node.h"


使用graph :: Node;

使用std :: cout;

使用std :: endl;


int main(){

节点a(a);

节点b( b;;


cout<< a<< "和 << b<< endl;

}

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::operator<<(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::ostream;
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;
}

推荐答案

basic_ostream @ DU?
basic_ostream@DU?


char_traits @ D @ std @@@ std @@ AAV12 @ AAVNode @ 0 @@ Z)

在函数_main Program.obj中引用


// Node.h

#ifndef NODE_H

#define NODE_H


#include< string>

#include< iostream>


使用std :: ostream;

使用std :: string;


命名空间图{

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"


使用graph :: Node;


命名空间图{

Node :: Node(void)

{

}

Node :: Node( const string id){

}


节点::〜节点(无效)

{

}


字符串Node :: getId()const {

返回id;

}

void Node :: setId(const string& id){

this-> id = id;

}

}


ostream& operator<<(ostream& output,Node& node){

output<< node.getId();

返回输出;

}


//Program.cpp

#include" Node.h"


使用graph :: Node;

使用std :: cout;

使用std :: endl;


int main(){

节点a(a);

节点b( b;;


cout<< a<< "和 << b<< endl;

}

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::ostream;
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;
}


#ifndef NODE_H

#define NODE_H


#include< string>

#include< iostream>


使用std :: ostream;

使用std :: string;


命名空间图{

class Node

{

公开:

朋友ostream& operator<<(ostream&,Node&);

Node(void);

Node (const string id);

~Node(void);


string getId()const;

void setId(const string&);

private:

string id;

};


}


#endif


//Node.cpp


使用graph :: Node;


命名空间图{

节点::节点(无效)

{

}


Node :: Node(const string id){

}


Node :: ~Node(void)

{

}


string Node :: getId()co nst {

返回id;

}


void Node :: setId(const string& id){

this-> id = id;

}


ostream& operator<<(ostream& output,Node&节点){

输出<< node.getId();

返回输出;

}

}


//程序.cpp


使用graph :: Node;

使用std :: cout;

使用std :: endl;


int main(){

节点a(" a");

节点b(" b");


cout<< a<< "和 << b<<结束;

}


修复它。

#ifndef NODE_H
#define NODE_H

#include <string>
#include <iostream>

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

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

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

}

#endif

//Node.cpp

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

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

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

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

Fixes it.


这篇关于名称空间,链接错误,运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆