菜鸟新学C++,问题求教

kangqin 2006-05-09 10:36:42
刚开始学,没人教自己看书真的问题多多呢,才写20行代码就出错,请高手指教

#include <iostream>
using namespace std;
template <class Type>
class term{
friend ostream & operator <<<Type>(ostream &,const term<Type> &);
private:
int col,row;
Type value;
public:
term(int x,int y,Type var):col(x),row(y),value(var){}
};

template <class Type> ostream & operator <<(ostream & out,const term<Type> & temp){
out<<"X:"<<temp.col<<" Y:"<<temp.row<<" Value:"<<temp.value<<endl;
return out;
};
int main(){
term<int> test(1,1,4);
cout<<test;
return 0;
}

friend里的template匹配不上,总报错,那应该如何写呢?
...全文
165 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kangqin 2006-05-09
  • 打赏
  • 举报
回复
放分。。。
kangqin 2006-05-09
  • 打赏
  • 举报
回复
bruceteen(周星星)的正解,其余同学的方法我都试过,没有成功,依赖编译器对学习来说还是不好,其实同样的代码我在MINGW下也编成功过。
不过还是感谢所有参与的朋友,放分了。
sharkoasis 2006-05-09
  • 打赏
  • 举报
回复
我用gcc 3.2.3编译通过了!虽然我也认为应该加个空格!gcc就是强啊!
bruceteen 2006-05-09
  • 打赏
  • 举报
回复
在using namespace std;之后,template <class Type> class term{ 之前加两句:

template <class Type> class term;
template <class Type> ostream& operator<<( ostream&, const term<Type>& );
adintr 2006-05-09
  • 打赏
  • 举报
回复
friend ostream & operator <<(ostream &,const term<Type> &);
铖邑 2006-05-09
  • 打赏
  • 举报
回复
friend ostream & operator <<<Type>(ostream &,const term<Type> &);
“<<<”加个空格变成---“<< <”

65,187

社区成员

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

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