fatal error LNK1169: one or more multiply defined symbols found

basecoding 2009-02-22 08:32:27
最近开始做数据结构方面的编程,简单的问题,却出现LINK方面的问题。
如果把main()里面的code放到LinkList.cpp里面去的话,编译是没问题的。
但是单独拿出来,再编译的话,就会出现fatal error LNK1169: one or more multiply defined symbols found

特来请教高人,先答出者,马上送20分。

//LinkList.h文件:

typedef int ElemType;
#define OK 1

//Á´±í½Óµã½á¹¹Ì嶨Òå [data *next]
// --------------
// |data | *next|
// --------------
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode, *pLinkList;

//Á´±íÀà½Ó¿Ú¶¨Òå
class LinkList
{
public:
LinkList();
~LinkList();
bool InitList();
bool DestroyList();
bool ClearList();
bool IsEmpty();
int GetLength();

int LocateElem(int elem);

bool SetNodeData(int position, ElemType newData);
bool GetNodeData(int position, ElemType &data);
bool GetNode(int position, LNode **node);

bool InsertNode(int beforeWhich, ElemType data);
bool DeleteNode(int position);
private:
pLinkList m_pList;
int m_length;
};



//LinkList.cpp
#include <iostream.h>
#include "LinkList.h"

LinkList::LinkList()
{
m_pList=NULL;
m_length=0;
}

//Ïú»ÙÁ´±í
bool LinkList::DestroyList()
{
if(!ClearList())
{
return false;
}

delete m_pList;

return true;
}

LinkList::~LinkList()
{
if(!DestroyList())
{
DestroyList();
}
}


//½«LÖØÖÃΪ¿Õ±í£¬Ç°ÌáÊÇÏßÐÔ±íLÒÑ´æÔÚ
bool LinkList::ClearList()
{
if (m_pList==NULL)
{
return false;
}

LNode *pTemp=NULL;
while(m_pList->next!=NULL)
{
pTemp=m_pList->next;//pTempÖ¸ÏòµÚÒ»¸ö½Úµã
m_pList->next=pTemp->next;
delete pTemp;
}

m_pList->next=NULL;
m_length=0;

return OK;
}


//³õʼ»¯£¬·ÖÅäÒ»¸öÍ·½Úµã
bool LinkList::InitList()
{
m_pList=new LNode;
if(m_pList==NULL)
return false;

m_pList->next=NULL;

return true;
}

//ÅжÏÁ´±íÊÇ·ñΪ¿Õ£¬ÊÇ¿ÕÔò·µ»Øtrue£¬·ñÔò·µ»Øfalse
bool LinkList::IsEmpty()
{
if(m_pList->next==NULL)
{
return true;
}
else
{
return false;
}
}

//·µ»ØÁ´±íÖе±Ç°½ÚµãÊý
int LinkList::GetLength()
{
return m_length;
}

//½«positionÖ¸¶¨µÄ½ÚµãÄÚµÄÊý¾ÝÉèÖÃΪnewData
//µÚ1¸öÓÐЧ½ÚµãµÄpostionΪ1
bool LinkList::SetNodeData(int position, ElemType newData)
{
LNode *pTemp=NULL;

if(!(GetNode(position,&pTemp)))
{
return false;
}

pTemp->data=newData;

return true;
}

//µÃµ½Ö¸¶¨Î»ÖýڵãµÄÖ¸Õë
bool LinkList::GetNode(int position, LNode **node)
{
LNode *pTemp=m_pList;
int curPos=0;

if (position<1 || position>m_length)
{
return false;
}

while (pTemp!=NULL)
{
curPos++;
if(curPos==position)
break;
pTemp=pTemp->next;
}

if(curPos!=position)
{
return false;
}

*node=pTemp;

return true;
}

//»ñÈ¡position´¦µÄdata
bool LinkList::GetNodeData(int position, ElemType &data)
{
LNode *pTemp=m_pList;
int curPos=0;

if(position<1 || position>m_length)
return false;

while(pTemp!=NULL)
{
if(curPos==position)
break;
pTemp=pTemp->next;
}

if(curPos!=position)
{
return false;
}

data=pTemp->data;

return true;
}

//ÔÚÁ´±íÖвåÈëÒ»¸ö½Úµã
//нڵã²åÈëÔÚbeforeWhich֮ǰ
//beforeWhichÈ¡ÖµÔÚ(1,length+1)Ö®¼ä
bool LinkList::InsertNode(int beforeWhich, ElemType data)
{
LNode *pTemp=NULL;

if(beforeWhich<1 ||beforeWhich>m_length+1)
{
return false;
}

if(!(GetNode(beforeWhich-1, &pTemp)))
{
return false;
}

LNode *newNode=new LNode;
newNode->data=data;
newNode->next=pTemp->next;

pTemp->next=newNode;

m_length++;

return true;
}


//main()函数:
#include <iostream.h>
#include "LinkList.cpp"

int main()
{
LinkList p;
p.InitList();
p.InsertNode(1,100);

return 1;
}
...全文
1361 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
basecoding 2009-02-23
  • 打赏
  • 举报
回复
非常感谢6楼的兄弟。
basecoding 2009-02-23
  • 打赏
  • 举报
回复
//linkList.cpp

#include <iostream.h>
#include "LinkList.h"

LinkList::LinkList()
{
m_pList=NULL;
m_length=0;
}

//Destory LinkList
bool LinkList::DestroyList()
{
if(!ClearList())
{
return false;
}

delete m_pList;

return true;
}

LinkList::~LinkList()
{
if(!DestroyList())
{
DestroyList();
}
}


//Clear LinkList
bool LinkList::ClearList()
{
if (m_pList==NULL)
{
return false;
}

LNode *pTemp=NULL;
while(m_pList->next!=NULL)
{
pTemp=m_pList->next;//pTemp
m_pList->next=pTemp->next;
delete pTemp;
}

m_pList->next=NULL;
m_length=0;

return OK;
}


//???,???????
bool LinkList::InitList()
{
m_pList=new LNode;
if(m_pList==NULL)
return false;

m_pList->next=NULL;

return true;
}

//????????,?????true,????false
bool LinkList::IsEmpty()
{
if(m_pList->next==NULL)
{
return true;
}
else
{
return false;
}
}

//??????????
int LinkList::GetLength()
{
return m_length;

}

//?position????????????newData
//?1??????postion?1
bool LinkList::SetNodeData(int position, ElemType newData)
{
LNode *pTemp=NULL;

if(!(GetNode(position,&pTemp)))
{
return false;
}

pTemp->data=newData;

return true;
}

//???????????
bool LinkList::GetNode(int position, LNode **node)
{
LNode *pTemp=m_pList;
int curPos=0;

while (pTemp)
{
if(curPos==position)
break;
curPos++;
pTemp=pTemp->next;
}

if(curPos!=position)
{
return false;
}

*node=pTemp;

return true;
}

//??position??data
bool LinkList::GetNodeData(int position, ElemType &data)
{
LNode *pTemp=m_pList;
int curPos=0;

if(position<1 || position>m_length)
return false;

while(pTemp)
{
if(curPos==position)
break;
pTemp=pTemp->next;
}

if(curPos!=position)
{
return false;
}

data=pTemp->data;

return true;
}

//??????????
//??????beforeWhich??
//beforeWhich???(1,length+1)??
bool LinkList::InsertNode(int beforeWhich, ElemType data)
{
LNode *pTemp=NULL;

if(beforeWhich<1 ||beforeWhich>m_length+1)
{
return false;
}

if(!(GetNode(beforeWhich-1, &pTemp)))
{
return false;
}

LNode *newNode=new LNode;
if(newNode==NULL)
return false;
newNode->data=data;
newNode->next=pTemp->next;
pTemp->next=newNode;

m_length++;

return true;
}


//LinkList.h


typedef int ElemType;
#define OK 1

//????????? [data *next]
// --------------
// |data | *next|
// --------------
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode, *pLinkList;

//???????
class LinkList
{
public:
LinkList();
~LinkList();
bool InitList();
bool DestroyList();
bool ClearList();
bool IsEmpty();
int GetLength();

int LocateElem(int elem);

bool SetNodeData(int position, ElemType newData);
bool GetNodeData(int position, ElemType &data);
bool GetNode(int position, LNode **node);

bool InsertNode(int beforeWhich, ElemType data);
bool DeleteNode(int position);
private:
pLinkList m_pList;
int m_length;
};



//Test.cpp
#include <iostream.h>
//#include "LinkList.cpp"
#include "LinkList.h"

int main()
{

LinkList p;
ElemType ele;

p.InitList();


p.InsertNode(1,10);


p.InsertNode(2,20);
p.InsertNode(3,30);

cout<<p.GetLength()<<endl;
cout<<"ddd"<<endl;

for (int i=1;i<=p.GetLength(); i++)
{
p.GetNodeData(i,ele);
cout<<ele<<",";
}

return 1;

}

//编译出来后,3个数怎么是很大的负数啊????
哪儿出错了??
alvinsunxiang 2009-02-22
  • 打赏
  • 举报
回复
把#include "LinkList.cpp"
改为

#include "LinkList.h"
basecoding 2009-02-22
  • 打赏
  • 举报
回复
但是把main()里面的#iostream.h去掉,仍然会报错啊。
basecoding 2009-02-22
  • 打赏
  • 举报
回复
LinkList.cpp里面有#include LinkList.h和iostream.h
main.cpp里面有:#include LinkList.cpp 和iostream.h
schlafenhamster 2009-02-22
  • 打赏
  • 举报
回复
是不是头文件被include2次了,把头文件写成:
#ifndf xxx_h
#define xxx_h
...原头文件
#endif
basecoding 2009-02-22
  • 打赏
  • 举报
回复
编译出来后,无任何输出。郁闷呢。
liuzxchina 2009-02-22
  • 打赏
  • 举报
回复
#include "LinkList.cpp" ?

#include "LinkList.h"

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧