一个友元函数的问题

happyjessica2008 happyjessica2008 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我建立的类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;

}

...全文

相关推荐

评论 17
收藏 已收藏
分享
返回顶部
happyjessica2008
C++ 语言 65,214 成员 250,514 内容