读书人

error LNK2005解决方法

发布时间: 2012-03-19 22:03:05 作者: rapoo

error LNK2005
List没问题 Term单独实现也没问题 放在一起就链接不了了。。。

C/C++ code
#include<iostream>using namespace std;class List;class Term{public:    Term() {c

C/C++ code
 
#include"list.h"
#include <iostream>
using namespace std;
void main()
{
Term a(2,3);
cout < <a;
cin.get();
}


1>正在链接...
1>main.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Term &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVTerm@@@Z) 已经在 list.obj 中定义
1>main.obj : error LNK2005: "public: class Term & __thiscall Term::operator=(class Term &)" (??4Term@@QAEAAV0@AAV0@@Z) 已经在 list.obj 中定义
1>main.obj : error LNK2005: "public: class Term & __thiscall Term::operator=(int)" (??4Term@@QAEAAV0@H@Z) 已经在 list.obj 中定义
1>main.obj : error LNK2005: "public: bool __thiscall Term::operator!=(class Term &)" (??9Term@@QAE_NAAV0@@Z) 已经在 list.obj 中定义
1>main.obj : error LNK2005: "public: bool __thiscall Term::operator==(class Term &)" (??8Term@@QAE_NAAV0@@Z) 已经在 list.obj 中定义
1>Py.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Term &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVTerm@@@Z) 已经在 list.obj 中定义
1>Py.obj : error LNK2005: "public: class Term & __thiscall Term::operator=(class Term &)" (??4Term@@QAEAAV0@AAV0@@Z) 已经在 list.obj 中定义
1>Py.obj : error LNK2005: "public: class Term & __thiscall Term::operator=(int)" (??4Term@@QAEAAV0@H@Z) 已经在 list.obj 中定义
1>Py.obj : error LNK2005: "public: bool __thiscall Term::operator!=(class Term &)" (??9Term@@QAE_NAAV0@@Z) 已经在 list.obj 中定义
1>Py.obj : error LNK2005: "public: bool __thiscall Term::operator==(class Term &)" (??8Term@@QAE_NAAV0@@Z) 已经在 list.obj 中定义

[解决办法]
list.h的最前面加上两句
#ifndef LIST_H
#define LIST_H

最后一行加上
#endif

欲知详情,请google“头文件卫士”
[解决办法]
list.h中只保留以下内容,其余移到list.cpp中:
C/C++ code
#ifndef LIST_H#define LIST_H#include<iostream>using namespace std;class List;class Term{public:    Term() {c=e=0;}    Term(int a,int b) {c=a;e=b;}    Term(int a) {c=e=a;}    int c,e;    friend ostream &operator<<(ostream &,Term &);    Term &operator=(Term &);    Term &operator=(int);    bool operator!=(Term &);    bool operator==(Term &);};#endif
[解决办法]
探讨
list.h中只保留以下内容,其余移到list.cpp中:

C/C++ code
#ifndef LIST_H
#define LIST_H
#include<iostream>
using namespace std;
class List;
class Term
{
public:
Term() {c=e=0;}
Term(int a,int b) {……

[解决办法]
应该是楼上的解决办法,我也遇到过
而且要是没有将文件添加到工程也会出现这种问题,
只有函数的声明,没有实现文件也会出现

读书人网 >C++

热点推荐