模板高手请进,急问!

danmao 2003-12-17 02:14:31
template <class Item>
class ListIterator
{
typedef std::list<Item> Listtype;
public:
ListIterator(const Listtype * aList){
alist = aList;
pos = aList->begin();
}



virtual ~ListIterator();

private:
Listtype * alist;
Listtype::iterator pos;
};
无法编译通过,什么原因?
linux中,错误信息如下:
In function `__default_alloc_template<true, 0>::_S_chunk_alloc(unsigned int, int &)':
/usr/include/g++-3/stl_alloc.h:480: undefined reference to `ListIterator<char *>::ListIterator(list<char *, allocator<char *> > const *)'
/usr/include/g++-3/stl_alloc.h:488: undefined reference to `ListIterator<char *>::~ListIterator(void)'
/usr/include/g++-3/stl_alloc.h:501: undefined reference to `ListIterator<char *>::~ListIterator(void)'
collect2: ld returned 1 exit status
我大概知道是没有正确的allocator,有没有高手给个例子之类?
...全文
52 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wolf0403 2003-12-17
  • 打赏
  • 举报
回复
C++ Primer 是说过模板分离编译,但是那个 export 在目前不被大多数主流编译器实现啊,呵
merlinran 2003-12-17
  • 打赏
  • 举报
回复
两个办法:
1、将函数体都写在头文件中。
2、在头文件中加上一行:template ListIterator<char*>
不过2只能解决当前的问题。一般都是全部全部写在头文件中。
LunTanZeng 2003-12-17
  • 打赏
  • 举报
回复
up!
abitz 2003-12-17
  • 打赏
  • 举报
回复
书上写的不一定准啊。

”虽然不一定非得在头文件里面,但是你用到的地方必须有实现“
这个对,但用起来比较恶心。
想一想多个文件用到这个头文件里的定义时的情况。
FrankZhang123 2003-12-17
  • 打赏
  • 举报
回复
虽然不一定非得在头文件里面,但是你用到的地方必须有实现
danmao 2003-12-17
  • 打赏
  • 举报
回复
不好意思,没有试,刚才查C++primer的时候看到了。

p697看就是。用分离编译模式。
abitz 2003-12-17
  • 打赏
  • 举报
回复
”顺便回复一下,模板类只要在的定义和实现不一定要在同一个文件中,只是构造函数需要在头文件中写出实现“

好像不是这样。
我的vs.net2003上就不行。
你试了吗?
abitz 2003-12-17
  • 打赏
  • 举报
回复
好像不是这样。
我的vs.net2003上就不行。
你试了吗?
arfi 2003-12-17
  • 打赏
  • 举报
回复
???你又不是没有写实现,我用VC6编译的时候报告的是 不能实例话一个抽象类。
需要把你封闭的
/* Item & CurrentItem();

bool isDone();

void Next();

void First();

bool isEmpty();

void Clear();

int Size();
*/
解开,每个函数前加上virtual
danmao 2003-12-17
  • 打赏
  • 举报
回复
顺便回复一下,模板类只要在的定义和实现不一定要在同一个文件中,只是构造函数需要在头文件中写出实现。
wlfjck 2003-12-17
  • 打赏
  • 举报
回复
up
abitz 2003-12-17
  • 打赏
  • 举报
回复
呵呵
danmao 2003-12-17
  • 打赏
  • 举报
回复
啊!!!

无地自容啊,这个最基本的忘了 T_T

谢谢!!
abitz 2003-12-17
  • 打赏
  • 举报
回复
问题就在这里。
模板类的定义和实现必须在同一个文件中。
看看这篇文章:
http://www.csdn.net/develop/Read_Article.asp?Id=21696
danmao 2003-12-17
  • 打赏
  • 举报
回复
唉,测试代码也给你吧。看看到底是什么原因。

#include <string>
#include <list>
#include <iostream>
#include "Iterator.hxx"
#include "ListIterator.hxx"

//#include <strstream>
//#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
//Iterator<string *>* ptrIter;
/*list<string> myList;
string a = "aaaaaa"; //a';
string b = "bbbbbbbbbb";//'b';
string c = "ccccccccccccccccc";//'c';
myList.push_back(a);
myList.push_back(b);
myList.push_back(c);
*/
list<int> myList;
myList.push_back(1);
myList.push_back(2);
myList.push_back(3);
list<int> *ptrList = &myList;

ListIterator<int> mylistIter(ptrList);

/* for(mylistIter.First(); !mylistIter.isDone(); mylistIter.Next())
{
cout <<(*mylistIter.CurrentItem()) << endl;
}

*/
cout << "OK!" << endl;
return EXIT_SUCCESS;
}
abitz 2003-12-17
  • 打赏
  • 举报
回复
定义和实现必须在同一个文件中。
danmao 2003-12-17
  • 打赏
  • 举报
回复
gcc还可以通过,但是这个不是问题,主要是为什么提示:
undefined reference to `ListIterator<char *>::ListIterator(list<char *, allocator<char *> > const *)'

……

我现在就卡在这里了 T_T
abitz 2003-12-17
  • 打赏
  • 举报
回复
1。模板参数是string/char/int吧?
list<string>/list<char>/list<int>这个不对。

2。有点象分离编译的问题。你的定义和实现是在一个文件里吗?
danmao 2003-12-17
  • 打赏
  • 举报
回复

#ifndef ITERATOR_HXX
#define ITERATOR_HXX

template <class Item>
class Iterator
{
public:

virtual Item & CurrentItem() = 0;

virtual bool isDone() = 0;

virtual void Next() = 0;

virtual void First() = 0;

virtual bool isEmpty() = 0;

virtual void Clear() = 0;

virtual int Size() = 0;

virtual ~Iterator() {}

protected:

Iterator(){}
};

#endif // ITERATOR_HXX


#ifndef LISTITERATOR_HXX
#define LISTITERATOR_HXX

#include <list>

#include "Iterator.hxx"

template <class Item>
class ListIterator : public Iterator<Item>
{
typedef std::list<Item> Listtype;
public:
ListIterator(const Listtype * aList);

/* Item & CurrentItem();

bool isDone();

void Next();

void First();

bool isEmpty();

void Clear();

int Size();
*/
virtual ~ListIterator();

private:
Listtype * alist;
Listtype::iterator pos;
};

#endif // LISTITERATOR_HXX


#include "ListIterator.hxx"

using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

template <class Item>
ListIterator<Item>::ListIterator(const ListIterator::Listtype * aList)
{
alist = aList;
pos = aList->begin();
}

template <class Item>
ListIterator<Item>::~ListIterator()
{

}

template <class Item>
Item & ListIterator<Item>::CurrentItem()
{
return (*pos);
}

template <class Item>
bool ListIterator<Item>::isDone()
{
return (pos == alist->end());
}

template <class Item>
void ListIterator<Item>::Next()
{
++pos;
}

template <class Item>
void ListIterator<Item>::First()
{
pos = alist->begin();
}

template <class Item>
bool ListIterator<Item>::isEmpty()
{
return alist->empty();
}

template <class Item>
void ListIterator<Item>::Clear()
{
alist->clear();
}

template <class Item>
int ListIterator<Item>::Size()
{
return alist->size();
}

基本上代码如上,分三个文件,测试代码没有加上。是什么原因呢?

abitz 2003-12-17
  • 打赏
  • 举报
回复
还有这句:
Listtype::iterator pos;
写成这样好点:typename Listtype::iterator pos;
你的能通过?我的vs.net没有typename就不行。
加载更多回复(3)

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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