error LNK2019: 无法解析的外部符号 "class std::basic_ostream

duyiwuer2009 2011-04-16 12:00:19
问题描述:用int类型取代template<class Type>一切正常,而用template<class Type>就出现代码中的问题,在VS2008和VS2010中测试,纠结了一天了,望高手指点,感激不尽

//CirQueue.h文件
#include <iostream>
using namespace std;
//循环队列类
template<class Type>
class CirQueue
{
public:
CirQueue(int size);
~CirQueue();
void Store(int n);//初始数据存入
friend ostream& operator<<(ostream& output,CirQueue<Type>& cq);
private:
int *data;
int maxSize;
int head,rear;
int length;
};
//构造函数
template<class Type>
CirQueue<Type>::CirQueue(int size)
{
if(size<1)
throw "空间大小有误!";
maxSize=size;
data=new int[maxSize];
head=0;
rear=0;
length=0;
}
//析构函数
template<class Type>
CirQueue<Type>::~CirQueue()
{
delete [] data;
maxSize=0;
head=rear=-1;
length=0;
cout<<"测试析构是否成功"<<endl;
}
//初始数据存入空队列
template<class Type>
void CirQueue<Type>::Store(int n)
{
if(n<1 || n>maxSize-1)
throw "输入的数据量大小小于1或超长!";
for(int i=0; i<n; i++)
{
cin>>data[i];
}
rear=n;
length=n;
}
//重载输出队列元素
template<class Type>
ostream& operator<<(ostream& output,CirQueue<Type>& cq)
{
output<<"测试一下"<<endl;
/*
int i=cq.head;
if(cq.length==0)
output<<"队列为空!"<<endl;
if(cq.length==1)
output<<"惟一元素:"<<cq.data[i]<<endl;
else
{
while(i!=cq.rear)
{
if(i==cq.head)
output<<"对头元素:"<<cq.data[i]<<endl;
else if(i==cq.rear-1)
output<<"对尾元素:"<<cq.data[i]<<endl;
else
output<<cq.data[i]<<endl;
i=(i+1)%cq.maxSize;//【妙】
}
}
*/
return output;
}

//CirQueue_main.cpp
#include <iostream>
using namespace std;
#include "CirQueue.h"

int main()
{
int size;
cout<<"请输入队列空间大小:";
cin>>size;
CirQueue<int> *cirQ;
try
{
cirQ=new CirQueue<int>(size);
}
catch(char *p)
{
cout<<p<<endl;
exit(1);
}
int n;
cout<<"初始数据存入"<<endl;
cout<<"请输入要存入的数据量大小:";
cin>>n;
try
{
cout<<"请输入这"<<n<<"个数:"<<endl;
cirQ->Store(n);
}
catch(char *p)
{
cout<<p<<endl;
exit(1);
}
int choice;
do
{
cout<<endl;
cout<<"1-进队"<<endl;
cout<<"2-出队"<<endl;
cout<<"3-返回对头元素"<<endl;
cout<<"4-返回队尾元素"<<endl;
cout<<"5-返回对头、队尾位置"<<endl;
cout<<"6-返回队列长度"<<endl;
cout<<"7-输出队列中的元素"<<endl;
cout<<"8-退出"<<endl;
cout<<endl;
cout<<"请输入选项:";
cin>>choice;
switch(choice)
{
case 7:
cout<<(*cirQ)<<endl;
break;
case 8:
break;
default:
cout<<"无此选项!"<<endl;
break;
}
}while(choice!=8);
delete cirQ;//①【重点】加上这个语句才执行析构函数

return 0;
}

/*
下面是连接错误(VS2008、VS2010)

1>CirQueue_main.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class CirQueue<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$CirQueue@H@@@Z),该符号在函数 __catch$_main$0 中被引用
1>F:\编程\CirQueue\Debug\CirQueue.exe : fatal error LNK1120: 1 个无法解析的外部命令
*/

...全文
3161 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
康州大雁 2011-06-24
  • 打赏
  • 举报
回复
那个友元函数在类声明里面少了template<class Type>
书虫 2011-04-16
  • 打赏
  • 举报
回复
声明友元函数时用
template<class Type>
friend ostream& operator<<(ostream& output,CirQueue<Type>& cq);
maoxing63570 2011-04-16
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
template<class Type>
class CirQueue;
template<class Type>
ostream& operator<<(ostream& output,CirQueue<Type>& cq);
//循环队列类
template<class Type>
class CirQueue
{
public:
CirQueue(int size);
~CirQueue();
void Store(int n);//初始数据存入
friend ostream& operator<< <Type>(ostream& output,CirQueue<Type>& cq);
private:
int *data;
int maxSize;
int head,rear;
int length;
};
//构造函数
template<class Type>
CirQueue<Type>::CirQueue(int size)
{
if(size<1)
throw "空间大小有误!";
maxSize=size;
data=new int[maxSize];
head=0;
rear=0;
length=0;
}
//析构函数
template<class Type>
CirQueue<Type>::~CirQueue()
{
delete [] data;
maxSize=0;
head=rear=-1;
length=0;
cout<<"测试析构是否成功"<<endl;
}
//初始数据存入空队列
template<class Type>
void CirQueue<Type>::Store(int n)
{
if(n<1 || n>maxSize-1)
throw "输入的数据量大小小于1或超长!";
for(int i=0; i<n; i++)
{
cin>>data[i];
}
rear=n;
length=n;
}
//重载输出队列元素
template<class Type>
ostream& operator<<(ostream& output,CirQueue<Type>& cq)
{
output<<"测试一下"<<endl;
/*
int i=cq.head;
if(cq.length==0)
output<<"队列为空!"<<endl;
if(cq.length==1)
output<<"惟一元素:"<<cq.data[i]<<endl;
else
{
while(i!=cq.rear)
{
if(i==cq.head)
output<<"对头元素:"<<cq.data[i]<<endl;
else if(i==cq.rear-1)
output<<"对尾元素:"<<cq.data[i]<<endl;
else
output<<cq.data[i]<<endl;
i=(i+1)%cq.maxSize;//【妙】
}
}
*/
return output;
}

这样玩下
luciferisnotsatan 2011-04-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]

template <class S>friend ostream& operator<< (ostream& output,CirQueue<S>& cq);
[/Quote]
++
pengzhixi 2011-04-16
  • 打赏
  • 举报
回复
template <class S>friend ostream& operator<< (ostream& output,CirQueue<S>& cq);
duyiwuer2009 2011-04-16
  • 打赏
  • 举报
回复
两种写法的原理有什么不同?
1、friend ostream& operator<< <>(ostream& output,CirQueue<Type>& cq);表示的是一种实现吗?
2、
template<class Type>
friend ostream& operator<<(ostream& output,CirQueue<Type>& cq);表示的是一种声明吗?
qq120848369 2011-04-16
  • 打赏
  • 举报
回复
friend ostream& operator<< <>(ostream& output,CirQueue<Type>& cq);
pengzhixi 2011-04-16
  • 打赏
  • 举报
回复
模板的解析如果照你那么写,意味着你friend ostream& operator<<(ostream& output,CirQueue<Type>& cq);
是in用的一个普通函数,但是很明显不是,那么就得用<>就是说<< 后面跟上<>来表示该函数是一个实例,当然我给出的是直接在里面声明该函数本身是一个模板,然后在外面定义即可
duyiwuer2009 2011-04-16
  • 打赏
  • 举报
回复
没想到各位同僚回得这么快,又google了几小时,问题解决了,总结如下:
1、在类里面声明友元函数时用
template<class Type>
friend ostream& operator<<(ostream& output,CirQueue<Type>& cq);
2、在里面声明友元函数时
ostream& operator<< <Type>(ostream& output,CirQueue<Type>& cq);//多加一个<Type>
不过在定义该函数时就不要加了
由于是用的午休时间在解决,所以有点浮躁,对于第二种之前见到过,但在定义中也加了一个<Type>,所以当时没解决;
对于原理,还没仔细思考,等思考后把心得分享一下,不过很期待各位高手对原理的解释
推荐一篇文章,里面讲了些原理:http://topic.csdn.net/u/20070910/14/291db915-dd3f-4bf1-9b95-72abc0387ff9.html

感受:
真没想到各位同僚会回答得这么即时,因为是第一次在论坛上提问,所以缺乏了解,非常感谢,哈哈
我们把同一标的(昆仑万维,现价 43.20 元,2026-07-31 收盘)交给三套系统,各出一份独立分析: **C 报告(CoordClaw 基于管理学多智能体系统)**——投研级。它由五个角色构成:周婷整合撰写、李静出基本面、王芳出技术面、赵明出风险、陈默做 PM 终审。最终产物是一份 38 项分级风险清单(P0×4 / P1×12 / P2×12 / P3×6 / 尾部×4)、双源交叉验证的财务数据(EM/Sina 差异 <0.01%)、严格的口径纪律,以及一份原样保留的"待核实"清单。结论冷冰冰:高风险,不建议参与。 **D 报告(DeepSeek)**——信息整理级。它把"4+3 AGI 战略"、天工 AI、Opera 浏览器、StarMaker 拆得很漂亮,核心财务数据(营收 81.98 亿、归母 -15.93 亿)也没算错。但整篇没有技术面、没有量化风控,更关键的是——它完全没提实控人已减持 75%、质押状态未知、净现金仅 15.19 亿且续航只有 1.26~1.81 年这些要命的负面。这是典型的"选择性呈现"。 **K 报告(Kimi)**——以对比评估的方式呈现。它搭起"数据准确性 / 分析维度 / 结论合理性"的三维框架,把几份材料放在一起对照,给出各自的强弱判定。它的维度意识比 D 报告更自觉,但作为一份独立分析,它对"评估方法本身的信度"交待不足,部分引用的核对也不够彻底。 结果两家的结论高度一致。C 报告(多智能体)被评投研级、居首;D 报告(DeepSeek 自己写的)被评信息整理级、居中;K 报告(Kimi 自己那份)维度较全但核验深度有限,排在两者之间。DeepSeek 的那份评估把 C 给了五星、D 三星、K 四星;Kimi 的那份评估也独立地把最高分给了 C。
问题现象 - 同一张 TF 卡/SD 卡在其他电脑上能正常识别,插到本机却只"响一声",资源管理器里不显示盘符; - 磁盘管理里能看到磁盘,但显示为灰色 / RAW / 无盘符; - 插入 U 盘、读卡器后偶尔能显示,重启或更换卡片后又消失; - 使用 diskpart、mountvol 等命令时系统卡住无响应。 ## 问题根源 经排查,这类问题通常不是 TF 卡损坏,而是 Windows 存储子系统与本机读卡器/驱动之间的兼容性故障,主要包括: 1. **盘符未分配** —— 卷已被系统识别,但没有挂载点,因此资源管理器不显示; 2. **USB 幽灵设备残留** —— 系统里残留 `Disconnected` 状态的 USBSTOR 设备记录,阻塞新设备枚举; 3. **USB 选择性暂停(Selective Suspend)** —— Windows 空闲时给读卡器断电,导致识别不稳定; 4. **automount 被关闭或失效** —— 新插入的卷无法自动分配盘符; 5. **存储堆栈挂起** —— 残留的 diskpart 进程锁死存储服务。 ## 解决方案(一键永久修复) 本项目是一个 **Codex Skill + 一键安装器**,自动完成以下修复: - 启用系统自动挂载(automount enable / scrub) - 清理 Disconnected 幽灵 USB 设备 - 永久禁用 USB 选择性暂停(防读卡器被断电) - 自动为所有无盘符的可移动卷分配盘符 - 注册**开机守护任务**:每次开机自动执行以上修复,彻底告别手动操作 ## 使用方法 ```powershell # 在其他电脑上(Windows 10/11): # 1. 下载本项目 # 2. 右键 install.bat → 以管理员身份运行(或直接双击后点"是")

65,212

社区成员

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

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