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 个无法解析的外部命令
*/

...全文
3179 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

感受:
真没想到各位同僚会回答得这么即时,因为是第一次在论坛上提问,所以缺乏了解,非常感谢,哈哈
内容概要:本文围绕基于物理场的动态模式分解(piDMD)展开研究,重点探讨其在流体力学、热传导及多物理耦合系统中的建模与应用。通过Matlab代码实现,将传统的动态模式分解(DMD)方法与物理场控制方程相结合,引入物理约束以增强数据驱动模型的物理一致性,从而提升对复杂非线性系统的模态提取精度与长期预测能力。文章系统阐述了piDMD的理论基础、数学推导过程与算法实现步骤,并结合典型物理场仿真案例(如Navier-Stokes方程驱动的流场演化)进行验证,展示了其在降阶建模、主导模态识别和时空动态可视化方面的优越性能,适用于高维、强耦合、非平稳系统的分析与优化。; 适合人群:具备一定数值计算、流体力学、控制理论或数据驱动建模背景,熟悉Matlab编程与线性代数运算,从事科学研究、工程仿真或系统辨识工作的研究生、科研人员及工程师。; 使用场景及目标:①实现对流场、温度场等物理场的高效降阶建模与动态行为预测;②提取符合物理规律的关键模态,用于系统稳定性分析与控制设计;③支撑复杂系统的状态估计、异常检测与优化调控等下游任务。; 阅读建议:建议读者结合提供的Matlab代码逐行调试与运行,深入理解piDMD算法中物理约束的嵌入方式及其对模态质量的影响,同时可尝试将其迁移至其他偏微分方程描述的物理系统中进行拓展验证与应用。
内容概要:本文研究了在峰谷分时电价引导下,基于多目标优化遗传算法NSGA-II的电动汽车充电负荷优化问题。通过构建以电网负荷波动最小化和用户充电成本最低为核心的多目标优化模型,利用NSGA-II算法求解获得帕累托最优解集,实现对大规模电动汽车充电行为的有序引导。文中系统阐述了峰谷电价激励机制的设计原理、电动汽车充电负荷的数学建模方法、多目标优化问题的形式化表达以及NSGA-II算法的具体实现流程,并借助Matlab平台进行仿真验证,结果表明该策略能有效平抑电网负荷峰谷差、降低用户充电支出并提升电力系统的运行效率与稳定性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力,从事智能电网、电动汽车调度、需求侧管理等相关领域的研究生或科研人员。; 使用场景及目标:①研究峰谷电价政策对电动汽车充电行为的影响机制;②掌握NSGA-II算法在电力系统多目标优化中的具体应用;③实现电动汽车集群的有序充电调度以改善电网负荷曲线; 阅读建议:此资源结合了电力经济学与智能优化算法,建议读者在学习过程中重点关注模型构建与算法实现的衔接部分,动手复现仿真代码以深入理解参数设置与结果分析过程。

65,213

社区成员

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

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