类模板问题,说error LNK2019: 无法解析的外部符号,请教。

ys_w 2006-11-03 11:59:35
test.h
#include <iostream>
using namespace std;

template <class Type>
class Queue{
public:
Queue():front(0),back(0){}
~Queue();

Type remove();
void add(const Type &);
bool is_empty() const{ return front == 0;}
friend ostream & operator << <Type>(ostream &,const Queue<Type> &q);
private:
class QueueItem{
public:
QueueItem(const Type &t):item(t),next(0){}
friend ostream & operator<< <Type>(ostream &,const QueueItem &);
Type item;
QueueItem *next;
};

QueueItem *front;
QueueItem *back;
};
template <class Type>
Queue<Type>::~Queue()
{
while (!is_empty())
remove();
}
template <class Type>
Type Queue<Type>::remove()
{
if (is_empty())
{
cerr << "empty cannot remove" << endl;
exit(-1);
}
QueueItem *pt = front;
front = front->next;

Type retval = pt->item;
delete pt;
return retval;
}
template <class Type>
void Queue<Type>::add(const Type &val)
{
QueueItem *pt = new QueueItem(val);

if (is_empty())
front = back = pt;
else
{
back->next = pt;
back = pt;
}
}

template <class Type>
ostream& operator<<( ostream &os, const Queue<Type> &q )
{
os << "< ";
Queue<Type>::QueueItem *p;
for ( p = q.front; p; p = p->next )
os << *p << " ";
os << " >";
return os;
}

template <class Type>
ostream & operator<<(ostream &os,const typename Queue<Type>::QueueItem &q)
{
os << "< ";
QueueItem *p;

for (p = q.front;p;p = p->next)
os << p->item;

os << " >";
return os;
}
main.cpp
#include <iostream>
#include <vector>
#include "test.h"
using namespace std;

int main()
{
Queue<int> qi ;
int ival;
for (ival = 0;ival < 10;++ival)
qi.add(ival);

cout << qi;
int err_cnt = 0;
for (ival = 0;ival < 10;++ival)
{
int qval = qi.remove();
if (ival != qval) err_cnt ++;
}
if (!err_cnt)
cout << "execute ok"<< endl;
else
cerr << "execute error " << endl;

return 0;
}

在VS2003环境下,编译出现:error LNK2019: 无法解析的外部符号,但是将main中的cout注释掉,则
编译通过,不明白。


...全文
1082 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
jixingzhong 2006-11-03
  • 打赏
  • 举报
回复
估计和 debug 文件是没有什么关系 ....

从没有遇到这样的问题,
唉 ~~

楼主 “节哀 ” ...
lw1a2 2006-11-03
  • 打赏
  • 举报
回复
清空debug目录,重新弄
OOPhaisky 2006-11-03
  • 打赏
  • 举报
回复
找了半天,没找到原因,这种链接错误很讨厌,错误信息也非常的不具有可读性。
healer_kx 2006-11-03
  • 打赏
  • 举报
回复
链接错误。。。郁闷了。。
healer_kx 2006-11-03
  • 打赏
  • 举报
回复
上面说错了,是临界错误。
healer_kx 2006-11-03
  • 打赏
  • 举报
回复
很难讲,如果是编译错误的话,往往和你的friend函数有关系。
Jokar 2006-11-03
  • 打赏
  • 举报
回复
将 test.h 中的

#include <iostream>
using namespace std;
去掉~


main.cpp 中改成

#include <iostream>
#include <vector>

using namespace std;

#include "test.h"


试试.............
ys_w 2006-11-03
  • 打赏
  • 举报
回复
template <class Type>
ostream& operator<<( ostream &os, const Queue<Type> &q )
{
os << "< ";
Queue<Type>::QueueItem *p;
for ( p = q.front; p; p = p->next )
os << p->item << " ";
os << " >";
return os;
}
在VS2003下编译通过,但如果想重载Queueitem的 cout << 应该怎么办呢?
lw1a2 2006-11-03
  • 打赏
  • 举报
回复
2005编译通过了,2003不知道
lw1a2 2006-11-03
  • 打赏
  • 举报
回复
template <class Type>
ostream& operator<<( ostream &os, const Queue<Type> &q )
{
os << "< ";
Queue<Type>::QueueItem *p;
for ( p = q.front; p; p = p->next )
os << p->item << " ";
os << " >";
return os;
}
sinall 2006-11-03
  • 打赏
  • 举报
回复
用内联的形式定义:

friend ostream & operator << <Type>(ostream &,const Queue<Type> &q);
=>
friend ostream& operator<<( ostream &os, const Queue<Type> &q )
{
os << "< ";
Queue<Type>::QueueItem *p;
for ( p = q.front; p; p = p->next )
os << *p << " ";
os << " >";
return os;
}

friend ostream & operator<< <Type>(ostream &,const QueueItem &);
=>
ostream & operator<<(ostream &os,const QueueItem &q)
{
os << "< ";
QueueItem *p;

for (p = q.front;p;p = p->next)
os << p->item;

os << " >";
return os;
}
taodm 2006-11-03
  • 打赏
  • 举报
回复
同样啊,都要用basic_ostream<CHAR_TYPE, CHAR_TRAIT> &
swimmer2000 2006-11-03
  • 打赏
  • 举报
回复
friend ostream & operator<< <Type>(ostream &,const Queue<Type> &q);
这句有问题.
taodm 2006-11-03
  • 打赏
  • 举报
回复
对了,重载<<不可以用ostream了
得template<typename CHAR_TYPE, typename CHAR_TRAIT>
basic_ostream<CHAR_TYPE, CHAR_TRAIT> &
operator<<(basic_ostream<CHAR_TYPE, CHAR_TRAIT> & Out, 。。。)
taodm 2006-11-03
  • 打赏
  • 举报
回复
现在的错误提示呢?
ys_w 2006-11-03
  • 打赏
  • 举报
回复
taodm(taodm) ,还是不行,在VS2003下,你看看如何编译才能通过?
taodm 2006-11-03
  • 打赏
  • 举报
回复
呃,VC6?
模板的友元,是有些别扭的,你这么写:
class QueueItem{
public:
QueueItem(const Type &t):item(t),next(0){}
friend ostream & operator<< <Type>(ostream &,const QueueItem &)
{
os << "< ";
QueueItem *p;

for (p = q.front;p;p = p->next)
os << p->item;

os << " >";
return os;

}
Type item;
QueueItem *next;
};
ys_w 2006-11-03
  • 打赏
  • 举报
回复
将 cout << qi;注释掉就行了,是函数
template <class Type>
ostream & operator<<(ostream &os,const typename Queue<Type>::QueueItem &q) 引起的问题。

testMain.cpp
正在链接...
testMain.obj : error LNK2019: 无法解析的外部符号 "?<<@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABVQueueItem@?$Queue@H@@@Z" (?<<@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABVQueueItem@?$Queue@H@@@Z) ,该符号在函数 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<<int>(class std::basic_ostream<char,struct std::char_traits<char> > &,class Queue<int> const &)" (??$?6H@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Queue@H@@@Z) 中被引用
Debug/TestProject.exe : fatal error LNK1120: 1 个无法解析的外部命令
taodm 2006-11-03
  • 打赏
  • 举报
回复
“无法解析的外部符号”,那后面是啥?
最重要的信息被你漏了,还咋让人帮你。
哎,问问题都不会问。

64,654

社区成员

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

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