类模板问题,说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注释掉,则
编译通过,不明白。


...全文
1115 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
  • 打赏
  • 举报
回复
“无法解析的外部符号”,那后面是啥?
最重要的信息被你漏了,还咋让人帮你。
哎,问问题都不会问。
内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。

65,211

社区成员

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

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