关于用G++编译 undefined reference to 的问题!
照着书上的数据结构算法编链表的,编译通不过。显示undefined reference to `AList<int>::insert(int const&)
书上的算法是这样的,先是一个抽象类ADT,
/***********Abstract_List.h******************/
template <class Elem> class List {
public:
//清空
virtual void clear( ) = 0;
//插入
virtual bool insert( const Elem& ) = 0;
//添加
virtual bool append( const Elem& ) = 0;
//移除
virtual bool remove( Elem& ) = 0;
。。。省略。。。。。
}
然后用数组方式实现链表
/***********Array_List.h*********************/
#include "Abstract_List.h"
#define DefaultListSize 200
template< class Elem > // Array-based list implementation
class AList : public List< Elem > { //继承抽象类
private:
int maxSize; //Maximum size of list
int listSize; //Actual number of elements in list
int fence; //Position of fence
Elem *listArray; // Array holding list elements
public:
AList( int size = DefaultListSize ) { //Constructor
maxSize = size;
listSize = fence = 0;
listArray = new Elem[maxSize];
}
~AList( ) { delete [] listArray; } //Destructor
void clear( ) {
delete [] listArray;
listSize = fence = 0;
listArray = new Elem[maxSize];
}
bool insert( const Elem& ); 1 //这三个函数在另外的Cpp中实现。
bool append( const Elem& ); 2
bool remove( Elem& ); 3
void setStart( ) { fence = 0; }
。。。省略。。。。。
}
/***************Array_List.cpp****************/
#include "Array_List.h"
分别为3个实现。
template <class Elem> // Insert at front of right partition
bool AList<Elem>::insert( const Elem& item ) {
if( listSize == maxSize ) return false; //List is full
for( int i = listSize; i > fence; i-- ) // Shift Elems up
listArray[i] = listArray[i-1]; // to make room
listArray[fence] = item;
listSize++; //Increment list size
return true;
}
template <class Elem> //Append Elem to end of the list
bool AList<Elem>::append( const Elem& item ) {
if( listSize == maxSize ) return false;
listArray[listSize++] = item;
return true;
}
//Remove and return first Elem in right partition
template <class Elem>
bool AList<Elem>::remove( Elem& it ) {
if( rightLength( ) == 0 ) return false; //Nothing in right
it = listArray[fence]; //Copy removed Elem
for( int i = fence; i < listSize - 1; ++i )//Shift them down
listArray[i] = listArray[i+1];
listSize--;
return true;
}
/***************************main.c**************/
#include "Array_List.h"
#include <iostream>
using namespace std;
int main( int argc, char* argv[] ) {
cout << "Hello world" << endl;
AList< int > ArrList( 100 );
return 0;
}
main函数声明了一个链表变量就报错了。
如果不声名变量的话,可以通过编译,输出hello world。
不知道这个是什么问题,求大虾指教。
四个文件都在同一目录下,编译命令用
g++ Array_main.cpp Array_List.cpp
出错信息为。
g++ -o abc Array_main.cpp Array_List.cpp
/tmp/cc5kzKtg.o:(.rodata._ZTV5AListIiE[vtable for AList<int>]+0xc): undefined reference to `AList<int>::insert(int const&)'
/tmp/cc5kzKtg.o:(.rodata._ZTV5AListIiE[vtable for AList<int>]+0x10): undefined reference to `AList<int>::append(int const&)'
/tmp/cc5kzKtg.o:(.rodata._ZTV5AListIiE[vtable for AList<int>]+0x14): undefined reference to `AList<int>::remove(int&)'
顺便求大虾指教一下怎么写个makefile文件出来,谢谢。