在Array模板中重载<<和>>的问题

elegant87 2008-11-15 08:34:24

//下面的程序在Dev_C++上编译通不过!
//提示: [Warning] friend declaration `std::ostream& operator<<(std::ostream&, Array<T>&)' declares a
//[Warning] (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
non-template function


#include <iostream>
#include <cstdlib>

using namespace std;

template <class T>
class Array
{
public:
~Array();
Array(int);
friend ostream& operator <<(ostream&, Array<T>&);
friend istream& operator >>(istream& ,Array<T>&);
private:
T *array;
int size ;
};

template <class T>
Array<T>::Array(int len)
{
array=new T[len];
size=len;
}

template <class T>
Array<T>::~Array()
{
delete[]array;
size=0;
}

template <class T>
ostream& operator <<(ostream& out,Array<T>& a)
{
cout<<"the all number is :"<<endl;
for(int i=0;i<a.size;i++)
{
out<<a.array[i]<<ends;
}
return out;
}

template <class T>
istream& operator >>(istream& in ,Array<T>& a)
{
for(int i=0;i<a.size;i++)
{
cout<<"input the "<<(i+1)<<" number"<<endl;
in>>a.array[i];
}
return in;
}

int main()
{
Array<int>IA(6);
cin>>IA;
cout<<IA<<endl;
system("PAUSE");
return EXIT_SUCCESS;

}



//我把头文件修改了一下!在VC6.0上成功运行!
//大家看看这是怎么回事嗯?
//我想用Dev_C++编译器,怎么修改呢?

#include <iostream.h>
#include <stdlib.h>

template <class T>
class Array
{
public:
~Array();
Array(int);
friend ostream& operator <<(ostream&, Array<T>&);
friend istream& operator >>(istream& ,Array<T>&);
private:
T *array;
int size ;
};

template <class T>
Array<T>::Array(int len)
{
array=new T[len];
size=len;
}

template <class T>
Array<T>::~Array()
{
delete[]array;
size=0;
}

template <class T>
ostream& operator <<(ostream& out,Array<T>& a)
{
cout<<"the all number is :"<<endl;
for(int i=0;i<a.size;i++)
{
out<<a.array[i]<<ends;
}
return out;
}

template <class T>
istream& operator >>(istream& in ,Array<T>& a)
{
for(int i=0;i<a.size;i++)
{
cout<<"input the "<<(i+1)<<" number"<<endl;
in>>a.array[i];
}
return in;
}

int main()
{
Array<int>IA(6);
cin>>IA;
cout<<IA<<endl;
system("PAUSE");
return EXIT_SUCCESS;

}


...全文
147 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzr4304061988012 2008-11-15
  • 打赏
  • 举报
回复

//一下VS2008编译通过;

#include <iostream>

using namespace std;

template <class T> class Array;
template <class T> ostream& operator <<(ostream& out,const Array<T>& a);
template <class T> istream& operator >>(istream& in,Array<T>& a);

template <class T>
class Array
{
public:
~Array();
Array(int);
T& operator[](int);
Array& operator =(Array<T>&);
friend ostream& operator << <T>(ostream& out,const Array<T>& a);
friend istream& operator >> <T>(istream& in,Array<T>& a);
T FindCenter(T); //搜索数据与定值相同的结点
private:
T *array;
int size ;
};

template <class T>
Array<T>::Array(int len)
{
array=new T[len];
size=len;
}

template <class T>
Array<T>::~Array()
{
delete[]array;
size=0;
}

template <class T>
Array<T>& Array<T>::operator =(Array<T>& b)
{
if(size!=b.size)
{
cout<<"can't use equal !"<<endl;
abort();
}

for(int i=0;i<size;i++)
array[i]=b.array[i];

return *this;
}

template <class T>
T& Array<T>::operator[](int len)
{
if(len<0||len>size)
{
cout<<"is out side !";
abort();
}
return array[len];
}

template <class T>
ostream& operator <<(ostream& out,const Array<T>& a)
{
cout<<"the all number is :"<<endl;
for(int i=0;i<a.size;i++)
{
out<<a.array[i]<<ends;
}
return out;
}

template <class T>
istream& operator >>(istream& in ,Array<T>& a)
{
for(int i=0;i<a.size;i++)
{
cout<<"input the "<<(i+1)<<" number"<<endl;
in>>a.array[i];
}
return in;
}

template<typename T>
T Array<T>::FindCenter(T data)
{
while(size)
{ if(size/2==1)
{
cout<<array[size/2]<<endl;
}
else
{
cout<<array[size/2-1]<<" or "<<array[(size/2)]<<endl;
}
}
}

int main()
{
Array<int>IA(6);
cin>>IA;
cout<<IA<<endl;
system("PAUSE");
return EXIT_SUCCESS;

}

elegant87 2008-11-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 lzr4304061988012 的回复:]
C/C++ code//LZ 8L 我看了这里改下,类里面。friend ostream&operator<<<T>(ostream&out,constArray<T>&a);
friend istream&operator>><T>(istream&in,Array<T>&a);
[/Quote]
改了还是一样的问题!怎么回事呢?
请你在再看看!谢谢了!
lzr4304061988012 2008-11-15
  • 打赏
  • 举报
回复

//LZ 8L 我看了这里改下,类里面。
friend ostream& operator << <T> (ostream& out,const Array<T>& a);
friend istream& operator >> <T>(istream& in,Array<T>& a);
lzr4304061988012 2008-11-15
  • 打赏
  • 举报
回复
2L的不对我贴错了,3,4L方法LZ试过没?
elegant87 2008-11-15
  • 打赏
  • 举报
回复

/*我按照2楼的方法,在类里面定义friend ostream& operator <<(ostream& out,const Array<T>& a);
和friend istream& operator >>(istream& in,Array<T>& a); 是运行成功!
先面的是我在类外定义,运行就有问题了啊!
提示:
[Linker error] undefined reference to `operator>>(std::istream&, Array<int>&)'
[Linker error] undefined reference to `operator<<(std::ostream&, Array<int> const&)' */

//大家帮忙看看吧!这个问题困惑我很久了!谢谢大家了!

#include <iostream>

using namespace std;

template <class T> class Array;
template <class T> ostream& operator <<(ostream& out,const Array<T>& a);
template <class T> istream& operator >>(istream& in,Array<T>& a);

template <class T>
class Array
{
public:
~Array();
Array(int);
T& operator[](int);
Array& operator =(Array<T>&);
friend ostream& operator <<(ostream& out,const Array<T>& a);
friend istream& operator >>(istream& in,Array<T>& a);
T FindCenter(T); //搜索数据与定值相同的结点
private:
T *array;
int size ;
};

template <class T>
Array<T>::Array(int len)
{
array=new T[len];
size=len;
}

template <class T>
Array<T>::~Array()
{
delete[]array;
size=0;
}

template <class T>
Array<T>& Array<T>::operator =(Array<T>& b)
{
if(size!=b.size)
{
cout<<"can't use equal !"<<endl;
abort();
}

for(int i=0;i<size;i++)
array[i]=b.array[i];

return *this;
}

template <class T>
T& Array<T>::operator[](int len)
{
if(len<0||len>size)
{
cout<<"is out side !";
abort();
}
return array[len];
}

template <class T>
ostream& operator <<(ostream& out,const Array<T>& a)
{
cout<<"the all number is :"<<endl;
for(int i=0;i<a.size;i++)
{
out<<a.array[i]<<ends;
}
return out;
}

template <class T>
istream& operator >>(istream& in ,Array<T>& a)
{
for(int i=0;i<a.size;i++)
{
cout<<"input the "<<(i+1)<<" number"<<endl;
in>>a.array[i];
}
return in;
}

template<typename T>
T Array<T>::FindCenter(T data)
{
while(size)
{ if(size/2==1)
{
cout<<array[size/2]<<endl;
}
else
{
cout<<array[size/2-1]<<" or "<<array[(size/2)]<<endl;
}
}
}

int main()
{
Array<int>IA(6);
cin>>IA;
cout<<IA<<endl;
system("PAUSE");
return EXIT_SUCCESS;

}
lzr4304061988012 2008-11-15
  • 打赏
  • 举报
回复
方法一把该函数定义为模板函数,这个模板函数之前要申明,否则编译器不理解。

方法二把该函数当作普通函数,不过这个普通函数是随着模板类的实例化而实例化的,就是依赖模板类的,必须在类中定义.
elegant87 2008-11-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lzr4304061988012 的回复:]
C/C++ code//再贴一遍方法一,刚刚帖错了;晕死;templat<classT>Array;
template<classT>ostream&operator<<(ostream&,Array<T>&);
template<classT>istream&operator>>(istream&,Array<T>&);
template<classT>classArray
{public:~Array();
Array(int);
friend ostream&operator<<(ostream&, Array<T>&);

friend istream&operator>>(istream&in,Array<T>&a) ;private:
T*array;intsize ; …
[/Quote]
请问一下这是什么原因呢?谢谢!
星羽 2008-11-15
  • 打赏
  • 举报
回复
//下面的程序在Dev_C++上编译通不过!
//提示: [Warning] friend declaration `std::ostream& operator<<(std::ostream&, Array<T>&)' declares a
//[Warning] (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
non-template function


----------

是warning 不是error
lzr4304061988012 2008-11-15
  • 打赏
  • 举报
回复

//再贴一遍方法一,刚刚帖错了;晕死;
templat <class T> Array;
template<class T> ostream & operator <<(ostream &,Array<T> &);
template<class T> istream & operator >>(istream& ,Array<T>&);
template <class T>
class Array
{
public:
~Array();
Array(int);
friend ostream& operator <<(ostream&, Array<T>&);

friend istream& operator >>(istream& in ,Array<T>& a) ;

private:
T *array;
int size ;
};
.......

lzr4304061988012 2008-11-15
  • 打赏
  • 举报
回复

//方法二没贴出来;
template <class T>
class Array
{
public:
~Array();
Array(int);
friend ostream& operator <<(ostream&, Array<T>&)
{......//定义;}


friend istream& operator >>(istream& ,Array<T>&);
{.....//dingyi;}
private:
T *array;
int size ;
};



lzr4304061988012 2008-11-15
  • 打赏
  • 举报
回复

//这个以前讨论过几次;

//方法一:
templat <class T> Array;
template<class T> ostream & operator <<(ostream &,Array<T> &);
template<class T> istream & operator >>(istream& ,Array<T>&);
template <class T>
class Array
{
public:
~Array();
Array(int);
friend ostream& operator <<(ostream&, Array<T>&);
{
cout<<"the all number is :"<<endl;
for(int i=0;i<a.size;i++)
{
out<<a.array[i]<<ends;
}
return out;
}

friend istream& operator >>(istream& in ,Array<T>& a)
{
for(int i=0;i<a.size;i++)
{
cout<<"input the "<<(i+1)<<" number"<<endl;
in>>a.array[i];
}
return in;
}

private:
T *array;
int size ;
};
.......

//方法二;
template <class T>
class Array
{
public:
~Array();
Array(int);
friend ostream& operator <<(ostream&, Array<T>&)
{

friend istream& operator >>(istream& ,Array<T>&);
private:
T *array;
int size ;
};


太乙 2008-11-15
  • 打赏
  • 举报
回复
只是警告而已嘛!

要不把错误贴出来!

65,213

社区成员

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

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