|
以下是编译情况:
[linuxsir@ecit ch05]$ g++ List.cpp use_List1.cpp -o use_List1
/tmp/ccOoZw5K.o(.text+0x7c): In function `main':
: undefined reference to `list<int>::display(std::basic_ostream<char, std::char_traits<char> >&)'/tmp/ccOoZw5K.o(.text+0x8d): In function `main':
: undefined reference to `list<int>::find(int)'
/tmp/ccOoZw5K.o(.text+0x12c): In function `main':
: undefined reference to `list<int>::display(std::basic_ostream<char, std::char_traits<char> >&)'/tmp/ccOoZw5K.o(.text+0x13d): In function `main':
: undefined reference to `list<int>::remove(int)'
/tmp/ccOoZw5K.o(.text+0x19a): In function `main':
: undefined reference to `list<int>::display(std::basic_ostream<char, std::char_traits<char> >&)'/tmp/ccOoZw5K.o(.text+0x1cf): In function `main':
: undefined reference to `list<int>::remove_front()'
/tmp/ccOoZw5K.o(.text+0x1e3): In function `main':
: undefined reference to `list<int>::display(std::basic_ostream<char, std::char_traits<char> >&)'/tmp/ccOoZw5K.o(.text+0x218): In function `main':
: undefined reference to `list<int>::remove_all()'
/tmp/ccOoZw5K.o(.text+0x22c): In function `main':
: undefined reference to `list<int>::display(std::basic_ostream<char, std::char_traits<char> >&)'/tmp/ccOoZw5K.o(.gnu.linkonce.t._ZN4listIiED1Ev+0xd): In function `list<int>::~list [in-charge]()':
: undefined reference to `list<int>::remove_all()'
/tmp/ccOoZw5K.o(.gnu.linkonce.t._ZN4listIiE12insert_frontEi+0x26): In function `list<int>::insert_front(int)':
: undefined reference to `list_item<int>::list_item[in-charge](int, list_item<int>*)'
/tmp/ccOoZw5K.o(.gnu.linkonce.t._ZN4listIiE10insert_endEi+0x33): In function `list<int>::insert_end(int)':
: undefined reference to `list_item<int>::list_item[in-charge](int, list_item<int>*)'
/tmp/ccOoZw5K.o(.gnu.linkonce.t._ZN4listIiE10insert_endEi+0xba): In function `list<int>::insert_end(int)':
: undefined reference to `list_item<int>::list_item[in-charge](int, list_item<int>*)'
/tmp/ccOoZw5K.o(.gnu.linkonce.t._ZN4listIiE6insertEP9list_itemIiEi+0x4e): In function `list<int>::insert(list_item<int>*, int)':
: undefined reference to `list_item<int>::list_item[in-charge](int, list_item<int>*)'
collect2: ld returned 1 exit status
[linuxsir@ecit ch05]$
头文件声明: 部分内联定义函数)
1 //一个通用的链表模板类封装在名字空间中
2
3 #ifndef LIST_H_
4 #define LIST_H_
5
6 #include <iostream>
7 using namespace std;
8
9 template < typename elemType >
10 class list_item
11 {
12 public:
13 list_item( elemType value,
14 list_item *item_to_link_to = 0 );
15
16 list_item() {}
17 elemType value() { return _value; }
18 list_item * next() { return _next; }
19
20 void next( list_item *link) { _next = link; }
21 void value( elemType new_value) { _value = new_value; }
22
23 private:
24 elemType _value;
25 list_item <elemType> *_next;
26 };
27
28
29 template < typename elemType >
30 class list
31 {
32 public:
33 list()
34 : _at_front(0), _at_end(0), _current(0), _size(0) { }
35 //注意,这里都赋值为0,等无素插入完后,就不是指为0了.
36 list( const list &rhs )
37 : _at_front(0), _at_end(0) {
38 insert_all(rhs); }
39 list & operator= ( const list &);
40 ~list() { remove_all(); }
41
42 void insert( list_item <elemType> *ptr, elemType value);
43 void insert_end( elemType value);
44 void insert_front( elemType value);
45 void insert_all( const list &rhs);
46
47 int remove( elemType value);
48 void remove_front();
49 void remove_all();
50
51 list_item <elemType> *find( elemType vlaue);
52 list_item <elemType> *next_iter();
53 list_item <elemType> *init_iter( list_item <elemType> *it);
54
55 void display( ostream &os = cout);
56
57 void concat( const list &);
58 void reverse();
59 int size() { return _size; }
60
61 private:
62 void bump_up_size() { ++_size; }
63 void bump_down_size() { --_size; }
64
65 list_item<elemType> *_at_front;
66 list_item<elemType> *_at_end;
67 list_item<elemType> *_current;
68 int _size;
69 };
70#endif
71 template <typename elemType>
72 inline list <elemType> & list <elemType>: perator= (const list <elemType> &rhs)
73 {
74 if( this != &rhs)
75 {
76 remove_all();
77 insert_all(rhs);
78 }
79 return *this;
80 }
这些函数的实现文件如下 部分)
3 #include "List.h"
4 #include <iostream>
5 using namespace std;
6
7 template <typename elemType>
8 list_item <elemType>::list_item(elemType value, list_item <elemType> *item)
9 :_value (value)
10 {
11 if( !item )
12 _next = 0;
13 else
14 {
15 _next = item->_next;
16 item->_next = this;
17 }
18 }
19
20 template <typename elemType>
21 void list <elemType>::remove_front()
22 {
23 if ( _at_front )
24 {
25 list_item <elemType> *ptr = _at_front;
26 _at_front = _at_front->next();
27
28 //不希望_current指向被删除的项
29 if( _current == ptr)
30 _current = _at_front;
31
32 bump_down_size();
33 delete ptr;
34 }
35 }
36
37 template <typename elemType>
38 void list <elemType>::remove_all()
39 {
40 while ( _at_front)
41 remove_front();
42 _size = 0;
43 _at_front = _at_end = 0;
44 }
45
应用文件:
1 // compile with List.cpp
2 //这是一个使用这个链表的一小部分
3
4 #include "List.h"
5 #include <iostream>
6 using namespace std;
7
8 int main ()
9 {
10 list <int> mylist;
11
12 for ( int ix=0; ix<10; ++ix)
13 {
14 mylist.insert_front(ix);
15 mylist.insert_end(ix);
16 }
17
18 cout << "OK: after insert_front() and insert_end() \n";
19
20 mylist.display();
21
22 list_item <int> *it = mylist.find(8);
23 cout << endl
24 << "Searching for the value 8: found it? "
25 << ( it ? " yes!\n" : " no!\n");
26
27 mylist.insert( it, 1024);
28 cout << "\n"
29 << "Inserting element 1024 following the value 8\n";
30
31 mylist.display();
32
33 int elem_cnt = mylist.remove(8);
34 cout << "\n"
35 << "Removed " << elem_cnt << " of the value 8\n";
36
37 mylist.display();
38
39 cout << "\n" << "Removed front element\n";
40 mylist.remove_front();
41
42 mylist.display();
43
44 cout << "\n" << "Removed all element\n";
45 mylist.remove_all();
46
47 mylist.display();
48
49 return 0;
50 }
为什么在声明的所有函数的实现文件中定义的函数都出错,而在头文件中内联的函数就没有出错呢? |
|