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

ws84643557 2012-05-07 06:04:40
照着书上的数据结构算法编链表的,编译通不过。显示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文件出来,谢谢。
...全文
1261 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xunxun 2012-05-08
  • 打赏
  • 举报
回复
类似模板分离编译问题快变日经贴了
ws84643557 2012-05-08
  • 打赏
  • 举报
回复
额,是这样的阿。
export是干什么用的?
不过有这样的答案也算ok了吧。
FrankHB1989 2012-05-07
  • 打赏
  • 举报
回复
基本上市面上所有的编译器都不支持模板分离编译。
export在C++11里被废了。
老老实实全写头文件里吧。
ws84643557 2012-05-07
  • 打赏
  • 举报
回复
但是如果把在Array_List.cpp文件里面的类方法的实现,放在Array_List.h 的类定义的下面的话,就可以编译成功了。不知道这是什么意思呢?
ws84643557 2012-05-07
  • 打赏
  • 举报
回复
用了头文件路径了还是同样的错误阿。
tao@tao-laptop:~/datastruct/List$ ls
Abstract_List.h Array_List.cpp Array_List.cpp~ Array_List.h Array_List.h~ Array_List.h.gch Array_main.cpp Array_main.cpp~
tao@tao-laptop:~/datastruct/List$ g++ -I /home/tao/datastruct/List Array_List.cpp Array_main.cpp
/tmp/ccWGGVb8.o:(.rodata._ZTV5AListIiE[vtable for AList<int>]+0xc): undefined reference to `AList<int>::insert(int const&)'
/tmp/ccWGGVb8.o:(.rodata._ZTV5AListIiE[vtable for AList<int>]+0x10): undefined reference to `AList<int>::append(int const&)'
/tmp/ccWGGVb8.o:(.rodata._ZTV5AListIiE[vtable for AList<int>]+0x14): undefined reference to `AList<int>::remove(int&)'
aopha 2012-05-07
  • 打赏
  • 举报
回复
g++ -I(头文件路径) -o abc Array_main.cpp Array_List.cpp

#makefile
百度
"找到了份挺好的中文 Makefile 的教程,非常详细清楚_ 大家共享_-其它UNIX系统【已迁移到IXPUB】-ITPUB论坛-it168旗下专业技术社区.mht"
ws84643557 2012-05-07
  • 打赏
  • 举报
回复
去吃饭先,苦逼阿,饭都还没吃。
opencv2.4.4移植到ARM 一、编译环境及库文件 linux环境:ubuntu 11.10 交叉编译:4.6.1 opencv: opencv-2.4.4 cmake: cmake-2.8.1-Linux-i386.tar.gz 二、opencv-2.4.4的移植过程 解压cmake-2.8.1-Linux-i386.tar.gz到/usr/local/ cmake-2.8.1 #export PATH= /usr/local/ cmake-2.8.1/bin:$PATH 用到的主要目录说明: 交叉编译工具链所在目录 /usr/local/arm-4.6.1/ 安装opencv的目录 /usr/local/ opencv源码所在目录 /usr/local /opencv-2.4.4 编译好的opencv库所在目录 /usr/local /opencv-2.4.4/build 默认安装目录为/usr/local #mkdir build #cd build #cmake-gui 选择源代码目录 /usr/local /opencv-2.4.4 选择Build目录 /usr/local /opencv-2.4.4/build 点击Configure,保持generator为Unix Makefiles,选择Specify options for cross-compiling,点击Next, Operating System填写arm-linux C Compilers填写/usr/local/arm-4.6.1//bin/arm-none-linux-gnueabi-gcc C++ Compilers填写/usr/local/arm-4.6.1//bin/ arm-none-linux-gnueabi -g++ 程序库的Target Root填写/usr/local/arm-4.6.1/ 然后点击Finish,开始configure 点击Generate生成Makefile(等configure完之后按钮才能点击) #make 错误一: Linking CXX executable ../../bin/opencv_createsamples ../../lib/libopencv_core.so: undefined reference to `clock_gettime' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFerrorHandler' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFrealloc' ../../lib/libopencv_core.so: undefined reference to `pthread_key_create 解决方法: 修改CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthread -lrt,重新编译,错误消除 错误二: Linking CXX executable ../../bin/opencv_createsamples ../../lib/libopencv_highgui.so: undefined reference to `_TIFFerrorHandler' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFrealloc' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFmalloc' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFmemcpy' ../../lib/libopencv_highgui.so: undefined reference to `TIFFOpen' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFfree' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFwarningHandler' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFmemcmp' ../../lib/libopencv_high

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧