请教个问题:为什么友元模板函数要加 <T> ? ysbcg(Hummer) 在吗?
//==============================box.h======================================
#ifndef BOX_H
#define BOX_H
class box
{
public:
box(double lv=1,double bv=1,double hv=1);
friend ostream& operator < <(ostream& out,const box& rbox);
friend istream& operator> > (istream& in , box& rbox);
protected:
double length;
double breadth;
double height;
};
#endif
//===================================box.cpp=============================
#include <iostream>
using namespace std;
#include "box.h "
box::box(double lv , double bv , double hv ):length(lv),breadth(bv),height(hv){}
ostream& operator < <(ostream& out,const box& rbox)
{
return out < < ' ' < <rbox.length < < ' ' < <rbox.breadth < < ' ' < <rbox.height ;
}
istream& operator> > (istream& in , box& rbox)
{
return in> > rbox.length > > rbox.breadth > > rbox.height ;
}
//=================================stack.h===========================
template <typename T> class stack
{
public:
stack():pHead(0){}
friend ostream& operator < < (ostream& out,const stack& rstack);
friend istream& operator> > (istream& in,stack& rstack);
private:
class Node
{
public:
T* pItem;
Node* pNext;
Node(T* pNew):pItem(pNew),pNext(0){}
Node():pItem(0),pNext(0){}
~Node()
{
delete pItem;
}
};
Node* pHead;
};
template <typename T> ostream& operator < < (ostream& out,const stack <T> & rstack)
{
out < < ' ' < <(rstack.pHead!=0);
return rstack.writeNode(out,rstack.pHead);
}
template <typename T> istream& operator> > (istream& in,stack <T> & rstack)
{
bool notEmpty;
in> > notEmpty;
if(notEmpty)
rstack.pHead=rstack.readNode(in);
else
rstack.pHead=0;
return in;
}
//==================================main.cpp=============================
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
#include "stack.h "
#include "box.h "
int main()
{
box boxes[10];
stack <box> boxstack;
const string boxfilename= "c:\\data.txt ";
ofstream outboxfile(boxfilename.c_str());
outboxfile < <boxstack;
outboxfile.close();
stack <box> copyboxstack;
ifstream inboxfile(boxfilename.c_str ());
inboxfile> > copyboxstack;
return 0;
}
编译错误为:
正在链接...
main.obj : error LNK2019: 无法解析的外部符号 "class std::basic_istream <char,struct std::char_traits <char> > & __cdecl operator> > (class std::basic_istream <char,struct std::char_traits <char> > &,class stack <class box> &) " (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$stack@Vbox@@@@@Z),该符号在函数 _main 中被引用
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 stack <class box> const &) " (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$stack@Vbox@@@@@Z),该符号在函数 _main 中被引用
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\Try\Debug\Try.exe : fatal error LNK1120: 2 个无法解析的外部命令
生成日志保存在“file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\Try\Try\Debug\BuildLog.htm”
Try - 3 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
//下面是 ysbcg(Hummer) 回复的==========================================
ysbcg(Hummer) ( ) 信誉:104 Blog 2007-2-3 12:23:40 得分: 0
你的stack声明中的friend函数写错了
应该为
friend ostream& operator < < <T> (ostream& out,const stack <T> & rstack);
friend istream& operator> > <T> (istream& in,stack <T> & rstack);
//=========================================================================
提问:
加了 <T> 之后原来错误确实没了,但还有些新错误,暂时不管。我现在想知道为什么要加 <T> ?
[解决办法]
后面的限定词怎么就被你无视了?
文章的“总结”才是决定性的言论