一个友元函数的问题
我建立的类List是一个列表类,程序中我用了类模板,类的定义如下:
template<class T>
class List
{
public:
List(int max);
~List();
int length() const;
void add(T new_item);
bool full() const;
void erase();
friend ostream& operator <<(ostream& outs,const List<T>& the_list);
private:
T *item; //列表的首地址
int max_length;
int current_length;
};
我把重载的<<运算符的函数设置为List类的友元,具体代码如下:
template<class T>
ostream& operator <<(ostream& outs,const List<T>& the_list)
{ //重载<<运算符,用<<运算符输出列表各元素的值
for(int i=0;i<the_list.current_length;i++)
{
outs<<the_list.item[i]<<endl;
}
return outs;
}
可是程序运行总是有4个错误,错误指向上面的代码,大概是说重载运算符函数不能调用私有成员,错误内容如下:
list.cpp
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(25) : error C2248: 'current_length' : cannot access private member declared in class 'List<int>'
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(19) : see declaration of 'current_length'
F:\C++程序设计\C++面向对象程序设计\14_4\list.cpp(8) : 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 List<int> &)' being compiled
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(27) : error C2248: 'item' : cannot access private member declared in class 'List<int>'
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(17) : see declaration of 'item'
F:\C++程序设计\C++面向对象程序设计\14_4\list.cpp(8) : 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 List<int> &)' being compiled
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(25) : error C2248: 'current_length' : cannot access private member declared in class 'List<char>'
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(19) : see declaration of 'current_length'
F:\C++程序设计\C++面向对象程序设计\14_4\list.cpp(14) : 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 List<char> &)' being compiled
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(27) : error C2248: 'item' : cannot access private member declared in class 'List<char>'
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(17) : see declaration of 'item'
F:\C++程序设计\C++面向对象程序设计\14_4\list.cpp(14) : 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 List<char> &)' being compiled
Error executing cl.exe.
list.obj - 4 error(s), 0 warning(s)
我感觉好像我设置的友元没起作用似的,请问为什么?我是新手,请尽可能详细的回答,多谢了。
完整的代码如下:
#include <iostream>
#include <cstdlib>
using namespace std;
template<class T>
class List
{
public:
List(int max);
~List();
int length() const;
void add(T new_item);
bool full() const;
void erase();
friend ostream& operator <<(ostream& outs,const List<T>& the_list);
private:
T *item; //列表的首地址
int max_length;
int current_length;
};
template<class T>
ostream& operator <<(ostream& outs,const List<T>& the_list)
{ //重载<<运算符,用<<运算符输出列表各元素的值
for(int i=0;i<the_list.current_length;i++)
{
outs<<the_list.item[i]<<endl;
}
return outs;
}
template<class T>
List<T>::List(int max):max_length(max),current_length(0)
{
item=new T[max];
}
template<class T>
List<T>::~List()
{
delete [] item;
}
template<class T>
int List<T>::length() const
{
return current_length;
}
template<class T>
void List<T>::add(T new_item)
{
if(full())
{
cout<<"error:adding to a full list.";
exit(1);
}
else
{
item[current_length]=new_item;
current_length=current_length+1;
}
}
template<class T>
bool List<T>::full() const
{
return current_length==max_length;
}
template<class T>
void List<T>::erase()
{
current_length=0;
}
//主函数体内容如下:
#include "list.h"
void main()
{
List<int> first_list(2);
first_list.add(1);
first_list.add(2);
cout<<"first list = \n"<<first_list;
List<char> second_list(10);
second_list.add('A');
second_list.add('B');
second_list.add('C');
cout<<"second list = \n"<<second_list;
}
template<class T>
class List
{
public:
List(int max);
~List();
int length() const;
void add(T new_item);
bool full() const;
void erase();
friend ostream& operator <<(ostream& outs,const List<T>& the_list);
private:
T *item; //列表的首地址
int max_length;
int current_length;
};
我把重载的<<运算符的函数设置为List类的友元,具体代码如下:
template<class T>
ostream& operator <<(ostream& outs,const List<T>& the_list)
{ //重载<<运算符,用<<运算符输出列表各元素的值
for(int i=0;i<the_list.current_length;i++)
{
outs<<the_list.item[i]<<endl;
}
return outs;
}
可是程序运行总是有4个错误,错误指向上面的代码,大概是说重载运算符函数不能调用私有成员,错误内容如下:
list.cpp
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(25) : error C2248: 'current_length' : cannot access private member declared in class 'List<int>'
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(19) : see declaration of 'current_length'
F:\C++程序设计\C++面向对象程序设计\14_4\list.cpp(8) : 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 List<int> &)' being compiled
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(27) : error C2248: 'item' : cannot access private member declared in class 'List<int>'
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(17) : see declaration of 'item'
F:\C++程序设计\C++面向对象程序设计\14_4\list.cpp(8) : 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 List<int> &)' being compiled
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(25) : error C2248: 'current_length' : cannot access private member declared in class 'List<char>'
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(19) : see declaration of 'current_length'
F:\C++程序设计\C++面向对象程序设计\14_4\list.cpp(14) : 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 List<char> &)' being compiled
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(27) : error C2248: 'item' : cannot access private member declared in class 'List<char>'
f:\c++程序设计\c++面向对象程序设计\14_4\list.h(17) : see declaration of 'item'
F:\C++程序设计\C++面向对象程序设计\14_4\list.cpp(14) : 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 List<char> &)' being compiled
Error executing cl.exe.
list.obj - 4 error(s), 0 warning(s)
我感觉好像我设置的友元没起作用似的,请问为什么?我是新手,请尽可能详细的回答,多谢了。
完整的代码如下:
#include <iostream>
#include <cstdlib>
using namespace std;
template<class T>
class List
{
public:
List(int max);
~List();
int length() const;
void add(T new_item);
bool full() const;
void erase();
friend ostream& operator <<(ostream& outs,const List<T>& the_list);
private:
T *item; //列表的首地址
int max_length;
int current_length;
};
template<class T>
ostream& operator <<(ostream& outs,const List<T>& the_list)
{ //重载<<运算符,用<<运算符输出列表各元素的值
for(int i=0;i<the_list.current_length;i++)
{
outs<<the_list.item[i]<<endl;
}
return outs;
}
template<class T>
List<T>::List(int max):max_length(max),current_length(0)
{
item=new T[max];
}
template<class T>
List<T>::~List()
{
delete [] item;
}
template<class T>
int List<T>::length() const
{
return current_length;
}
template<class T>
void List<T>::add(T new_item)
{
if(full())
{
cout<<"error:adding to a full list.";
exit(1);
}
else
{
item[current_length]=new_item;
current_length=current_length+1;
}
}
template<class T>
bool List<T>::full() const
{
return current_length==max_length;
}
template<class T>
void List<T>::erase()
{
current_length=0;
}
//主函数体内容如下:
#include "list.h"
void main()
{
List<int> first_list(2);
first_list.add(1);
first_list.add(2);
cout<<"first list = \n"<<first_list;
List<char> second_list(10);
second_list.add('A');
second_list.add('B');
second_list.add('C');
cout<<"second list = \n"<<second_list;
}
...全文
相关推荐
友元函数详细讲解
友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数。类授予它的友元特别的访问权。通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你的类时,还要征得其它部分的拥有者的同意)。
C ++友元函数的理解
(1) 定义一个Boat和Car两个类,他们都具有私用属性--重量; (2)编写一个函数,计算两者的重量和。
关于C++友元函数的实现讲解
今天小编就为大家分享一篇关于关于C++友元函数的实现讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
C++友元函数的分析和理解
NULL 博文链接:https://jacky-dai.iteye.com/blog/2305426
自己体会的友元函数的理解
以前总是对friend感到迷茫,其实很简单,自己总结了一下,欢迎交流


