关于用G++编译 undefined reference to 的问题!

ws84643557 ws84643557 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
照着书上的数据结构算法编链表的,编译通不过。显示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文件出来,谢谢。
...全文

相关推荐

undefined reference to 'pthread_create'的解决方法

undefined reference to 'pthread_create'的解决方法,需要的朋友可以参考一下

weixin_38523728

undefined reference to ‘pthread_create’的解决方法

照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误: undefined reference to ‘pthread_create’undefined reference to ‘pthread_join’ 问题原因:    pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。 问题解决:    在编译中要加 -lpthread参数    gcc thread.c -o thread -lpthr

weixin_38698860

C生成静态库给C++调用出现undefined reference to ...解决方案_cpp

C生成静态库给C++调用出现undefined reference to ...解决方案 已经写好makefile,还有头文件,看注释,基本没问题的

周楷雯

undefined reference to `SetPduPowerConsumptionCnt'错误的解决方法

编译时出现undefined reference to `SetPduPowerConsumptionCnt'错误要如何解决呢?有没有什么好的解决方法?下面小编就为大家解答吧,如果你也遇到了这种情况,可以过来参考下

weixin_38706197

FFmpeg编译出错undefined reference to `img_convert’的解决方法

调用ffmpeg库函数,实现播放视频,编译时img_convert函数出错,提示找不到,是因为版本问题。新版本的ffmpeg添加了新的接口:libswscale来处理图像缩放。

lucystar
评论 7
收藏 已收藏
分享
返回顶部
ws84643557
C++ 语言 65,214 成员 250,514 内容