fatal error LNK1169: one or more multiply defined symbols found
最近开始做数据结构方面的编程,简单的问题,却出现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;
}