還是我做的那個鏈表的模板類
我分別寫了頭文件Chain.h,源文件Chain.cpp,并且在main.cpp中進行測試。
但是在連接的時候出現了問題,不知道什么原因,希望能夠有高手指點一下,非常感謝!
其中Chain.h聲明了類的數據成員和成員函數,具體內容如下:
#ifndef _CHAIN_H
#define _CHAIN_H
#include <iostream>
using namespace std;
template<class T>
class Chain;
template<class T>
class ChainNode
{
?friend class Chain<T>;
?friend ostream& operator<<(ostream& out, const Chain<T>& x);
private:
?T data;
?ChainNode<T> *link;
};
template<class T>
class Chain{
public:
?Chain(int p) {first = 0;};
?~Chain();
?bool IsEmpty() const {return first == 0;}
?int Length() const;
?bool Find(int k, T& x) const;
?int Search(const T& x) const;
?//Chain<T>& Delete(int k, T& x);
?Chain<T>& Insert(int k, const T& x);
?void Output(ostream& out = cout) const;
private:
?ChainNode<T> *first; // 指向第一個節點的指針
};
template<class T>
ostream& operator<<(ostream& out, const Chain<T>& x);
#endif? // _CHAIN
??在Chain.cpp中對Chain.h中聲明的函數進行了定義,具體內容如下:
#include "Chain.h"
#include <iostream>
using namespace std;
template<class T>
Chain<T>::~Chain()
{
?//鏈表的析構函數,用于刪除鏈表中的所有節點
?ChainNode<T> *ptr = first;
?while (ptr)
?{
??first = ptr->link;
??delete ptr;
??ptr = first;
?}
}
template<class T>
int Chain<T>::Length() const
{
?//返回鏈表中的元素總數
?int count = 0;
?ChainNode<T> *ptr = first;
?while (ptr)
?{
??++count;
??ptr = ptr->link;
?}
?return count;
}
template<class T>
bool Chain<T>::Find(int k, T& x) const
{
?//尋找鏈表中的第k個元素,并將其傳送至x
?//如果不存在第k個元素,則返回false,否則返回true
?if (k < 1)
?{
??return false;
?}
?int count = k;
?ChainNode<T> *ptr = first;
?while (count && ptr)
?{
??--count;
??ptr = ptr->link
?}
?if (!ptr)
?{
??return false;
?}
?x = ptr->data;
?return true;
}
template<class T>
int Chain<T>::Search(const T& x) const
{
?//尋找x,如果發現x,則返回x的地址
?//如果x不在鏈表中,則返回0
?int count = 1;
?ChainNode<T> *ptr = first;
?while(ptr && (ptr->data!=x))
?{
??++count;
??ptr = ptr->link;
?}
?if (ptr)
?{
??return count;
?}
?else
?{
??return 0;
?}
}
template<class T>
void Chain<T>::Output(ostream& out = cout) const
{
?ChainNode<T> *ptr = first;
?while (ptr)
?{
??out<<ptr->data<<"? ";
??ptr = ptr->link;
?}
}
//重載<<運算符
template<class T>
ostream& operator<<(ostream& out, const Chain<T>& x)
{
?x.Output(out);
?return out;
}
template<class T>
Chain<T>& Chain<T>::Insert(int k, const T& x)
{
?ChainNode<T> *ptr = first;
?int count = 0;
?while (ptr && count < k)
?{
??++count;
??ptr = ptr->link;
?}
?if (!ptr)? //如果沒到第k個節點
?{
?
?}
?else
?{
??//要插入的新節點
??ChainNode<T>* cn = new ChainNode<T>;
??cn->data = x;
??cn->link = 0;
??if (ptr->link==0)? //到達了鏈表的結尾
??{
???ptr->link = cn;
??}
??else? //沒到達結尾
??{?
???cn->link = ptr->link;
???ptr->link = cn;
??}
?}
?return *this
}
?在main.cpp中進行測試,測試的內容很少,但是剛開始就進行不了了。main.cpp內容如下:
#include "Chain.h"
using namespace std;
int main()
{
?Chain<int> c;
?cout<<c.Length();
?return 0;
}
編譯的時候沒有問題,但是在連接的時候就出現了問題,報錯如下:
--------------------Configuration: Chain - Win32 Debug--------------------
Linking...
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Chain<int>::~Chain<int>(void)" (??1?$Chain@H@@QAE@XZ)
main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Chain<int>::Length(void)const " (?Length@?$Chain@H@@QBEHXZ)
Debug/Chain.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
Chain.exe - 3 error(s), 0 warning(s)
但是從報錯信息來看,應該是在main.cpp中沒有找到所用到的函數 ~Chain<int>(void)和Length()的定義,在main.cpp中一共用到了三個函數,構造函數Chain(),但是構造函數是在Chain.h中定義的,所以編譯器找到了其定義,但是另外兩個函數是在Chain.cpp中定義的,而且目前報錯沒有找到,但是如果在main.cpp中引入#include "Chain.cpp"時,編譯和連接就沒有問題,這就證實了原來的估計是沒有錯的。我實在是不知道問題出現在哪里,所以希望哪位高手看出問題來的話,請告訴我,多謝了!
posted on 2006-06-19 16:56
Bourne 閱讀(841)
評論(4) 編輯 收藏 引用