| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1175 人关注过本帖
标题:关于类模板中使用友元input/output操作符
取消只看楼主 加入收藏
karlzhouzhi
Rank: 1
等 级:新手上路
帖 子:70
专家分:0
注 册:2005-12-25
收藏
 问题点数:0 回复次数:0 
关于类模板中使用友元input/output操作符

源程序:
template<int hi,int wid>
class Screen
{
public:
Screen():_cursor(0),_Screen(hi*wid,'#'){}
void home(){_cursor=0;}
void move(int,int);
char get(){return _Screen[_cursor];}
char get(int,int);
bool checkRange(int,int);
void copy(const Screen&);
void set(char c){_Screen=string(hi*wid,c);}
void set(const string&);
void set(const Screen&);

friend ostream& operator<<(ostream &out,const Screen<hi,wid> &s);
friend istream& operator>>(istream &in,Screen<hi,wid> &s);

private:
string _Screen;
int _cursor;
};

template<int hi,int wid>
ostream& operator<<(ostream &out,const Screen<hi,wid> &s)
{
for(int i=1;i<=hi;++i)
{
for(int j=1;j<=wid;++j)
out<<s.get(i,j); //C2663: 'get' : 2 overloads have no legal conversion for 'this' pointer
cout<<endl;
}
cout<<endl;
return out;
}


template<int hi,int wid>
istream& operator>>(istream &in,Screen<hi,wid> &s)
{
s.home();
for(int i=1;i<=hi;++i)
for(int j=1;j<=wid;++j)
in>>s._Screen[s._cursor++]; //友元为什么不能访问私有成员
return in;
}

template<int hi,int wid>
void Screen<hi,wid>::move(int row,int col)
{
if(checkRange(row,col))
{
int r=(row-1)*wid;
_cursor=col-1+r;
}
}


template<int hi,int wid>
bool Screen<hi,wid>::checkRange(int row,int col)
{
if(row<1||row>hi||col<1||col>wid)
{
cerr<<"System coordinate("
<<row<<","<<col
<<") out of bounds.\n";
return false;
}
return true;
}

template<int hi,int wid>
char Screen<hi,wid>::get(int r,int c)
{
move(r,c);
return get();
}

template<int hi,int wid>
void Screen<hi,wid>::copy(const Screen &sobj)
{
if(this!=&sobj)
{
_cursor=sobj._cursor;
_Screen=sobj._Screen;
}
}

template<int hi,int wid>
void Screen<hi,wid>::set(const Screen &s)
{
for(int r=1;r<=hi;++r)
for(int c=1;c<wid;++c)
{
move(r,c);
_Screen[_cursor]=s[_cursor];
}
}

template<int hi,int wid>
void Screen<hi,wid>::set(const string &s)
{
for(int r=1;r<=hi;++r)
for(int c=1;c<=wid;++c)
{
move(r,c);
_Screen[_cursor]=s[_cursor];
}
}

int main(void)
{
Screen<2,3> s1;
cout<<s1;

cout<<"please enter 6(chars):";
cin>>s1;
cout<<s1;


return 0;
}

功能描述:
我定义了一个类模板Screen<hi,wid>,想通过类模板中的friend input&output操作符输出Screen<2,3> s1中的内容
但是,友元的定义有问题:
问题1:error C2663: 'get' : 2 overloads have no legal conversion for 'this' pointer
d:\programe\c++ primer\test3\main.cpp(26) : see reference to function template instantiation 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl operator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,
const class Screen<2,3> &)' being compiled
问题2: error C2248: '_Screen' : cannot access private member declared in class 'Screen<2,3>'
d:\programe\c++ primer\test3\screen.h(29) : see declaration of '_Screen'

谢谢:)

搜索更多相关主题的帖子: 用友 操作符 output input 模板 
2006-05-16 15:06
快速回复:关于类模板中使用友元input/output操作符
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026141 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved