| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 841 人关注过本帖
标题:友元函数的问题
只看楼主 加入收藏
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
 问题点数:0 回复次数:4 
友元函数的问题
我在写链表的程序的时候出现问题,大家帮我看看
//List.h
#ifndef LIST_H_
#define LIST_H_
template <class Type>class List;
template <class Type>class ListNode
{
public:
friend class List<Type>;
ListNode(); //不给数据的节点构造函数
ListNode(const Type & item); //带数据的构造函数
ListNode <Type> *NextNode(){return *link;} //返回当前节点的下一个节点的地址
void InsertAfter(ListNode<Type> *node); //插入一个新的节点
ListNode <Type> *GetNode(const Type & item,ListNode<Type> *next=NULL);
//建立一个新的节点
ListNode<Type> *RemoveAfter(); //删除当前节点的下一个节点
private:
Type data; //当前节点的值
ListNode<Type> *link; //存放后继节点的地址
};
template <class Type>class List
{
List(const Type & value); //链表的构造函数
~List(); //析构函数
void ClearList(); //将链表置为空表,并释放原链表的节点空间
int ListLength()const; //计算链表的表长
ListNode<Type> *FindNode(Type value); //在链表中查找值为value的节点
ListNode<Type> *Find(int i); //搜索链表中第i个元素的地址
bool InsertNode(Type value,int i); //将新元素value插入在链表中第i个位置
Type *Remove(int i); //将链表中的第i个元素删除
Type *Get(int i); //取出链表中的第i个元素
private:
ListNode<Type> *first,*last; //链表的头指针和尾指针
};
template<class Type>
ListNode<Type>::ListNode(const Type & item):data(item),link(NULL)
{
}
template<class Type>
ListNode<Type>::ListNode():link(NULL)
{
}
template<class Type>
ListNode<Type> *ListNode<Type>::GetNode(const Type & item,ListNode<Type> *next=NULL)
{
//以数据成员item和指针next为参数,建立一个新的节点,函数返回新节点的地址
ListNode<Type> *newnode=new ListNode<Type>(item);
newnode->link=next;
return newnode;
}
template<class Type>
ListNode<Type>*ListNode<Type>::RemoveAfter()
{
//从链表中摘下当前节点的下一节点,并返回需删除的节点的地址
ListNode<Type> *tempptr=link;
if(link==NULL)
return NULL;
link=tempptr->link;
return tempptr;
}
template<class Type>
void ListNode<Type>::InsertAfter(ListNode<Type> *node)
{
//将node所指示的节点连接成为当前节点的后继节点
node->link=link;
link=node;
}
template<class Type>
List<Type>::List(const Type & value)
{
first=last=new ListNode<Type>(value);
}
template<class Type>
List<Type>::~List()
{
ClearList();
delete first;
}
template<class Type>
void List<Type>::ClearList()
{
//将链表置为空表
while (first->link!=NULL) //当链表不为空时,逐个删除出头结点以外的所有节点
{
ListNode<Type> *q=new ListNode<Type>;
q=first->link;
first->link=q->link;
delete q;
}
last=first;
}
template <class Type>
int List<Type>::ListLength()const
{
int count=0;
ListNode<Type> *p=first->link;
while(p!=NULL)
{
p=p->link;
count++;
}
return count;
}
template <class Type>
ListNode<Type> *List<Type>::FindNode(Type value)
{
//在链表中搜索数据为value的节点,搜索成功时,函数返回该节点地址,否则返回NULL
ListNode<Type> *p=first->link;
while(p->data!=value&&p!=NULL)
p=p->link;
return p;
}
template <class Type>
ListNode<Type> *List<Type>::Find(int i)
{
//定位函数,返回链表中第i个元素的地址,如果i<-1或i>链表的节点个数,则返回NULL
if(i<-1&&i>ListLength())
return NULL; //i值不合理
if(i==-1)
return first; //当i==-1时函数返回头结点
ListNode<Type> *p=first->link;
int count=0;
while(p!=NULL&&count<i)
{
p=p->link;
count++;
}
return p;
}
template <class Type>
bool List<Type>::InsertNode(Type value,int i)
{
//定位第i-1个元素的地址
ListNode<Type> *p=Find(i-1);
if(p==NULL)
return false;
ListNode<Type> *newnode=p->GetNode(value,p->link);
if (p->link==NULL)
{
p->link=newnode;
}
return true;
}
template <class Type>
Type *List<Type>::Remove(int i)
{
//在删除节点前先定位到第i-1个节点
ListNode<Type> *p=Find(i-1);
ListNode<Type> *q;
if (p==NULL||p->link==NULL)
{
return NULL; //i值不合理或为空表,返回NULL
}
q=p->link;
p->link=q->link; //q指向被删除节点
Type *value=new Type(q->data); //取出被删除节点的数据值
if (q==NULL)
last=p;
delete q;
return value;
}
template <class Type>
Type *List<Type>::Get(int i)
{
//取出链表中的第i个元素
ListNode<Type> *p=Find(i);
if(p==NULL||p==first)
return NULL;
else
return p->data;
}
#endif
//TestList.cpp
#include <iostream>
#include "List.h"
using namespace std;
int main()
{
cout<<"构造一个单链表:\n";
cout<<"请确定链表头结点的值:\n";
int headvalue;
cin>>headvalue;
List<int> list1(headvalue);
cout<<"请确定链表的长度:\n";
int n;
cin>>n;
int i=0;
while (i<n)
{
int value;
cout<<"请输入第"<<i+1<<"个节点的值"<<endl;
cin>>value;
list1.InsertNode(value,i+1);
i++;
}
cout<<"链表构造完毕,请选择你需要进行的操作:\n";
cout<<"1:插入一个新的节点\n";
cout<<"2:搜索含数据value的节点\n";
cout<<"3:搜索链表中第i个元素的地址\n";
cout<<"4:将新的元素插入到链表中的第i个位置\n";
cout<<"5:将链表中的第i个元素删除\n";
cout<<"6:取出链表中的第i个元素\n";
cout<<"0:退出\n";
int choice;
cin>>choice;
while(choice)
{
switch(choice)
{
case 1:
{
int value;
cout<<"请输入第"<<i+1<<"个节点的值"<<endl;
cin>>value;
list1.InsertNode(value,i+1);
break;
}
case 2:
{
cout<<"请输入你要搜索的元素的值:\n";
int value;
cin>>value;
ListNode<int> *p;
p= list1.Find(value);
if(p)
cout<<value<<"元素所在的地址为"<<p<<endl;
else
cout<<"操作为成功,请检查输入\n";
break;
}
case 3:
{
cout<<"请输入你要搜索的元素在表中的为位置\n";
int pos;
cin>>pos;
ListNode<int> *p;
p=list1.Find(pos);
if(p)
cout<<"第"<<pos<<"号元素的地址为"<<p<<endl;
else
cout<<"操作为成功,请检查输入\n";
break;
}
case 4:
{
int value,i;
cout<<"请输入新元素的值以及插入的位置\n";
cin>>value>>i;
if(list1.InsertNode(value,i))
cout<<"插入成功\n";
else
cout<<"操作不成功,请重试\n";
break;
}
case 5:
{
cout<<"请输入您要删除的元素在链表中的位置:\n";
int pos;
cin>>pos;
int *val;
val=list1.Remove(pos);
if (val)
cout<<"删除成功"<<endl;
else
cout<<"删除不成功"<<endl;
break;
}
case 6:
{
cout<<"请输入您要取出的元素在链表中的位置:\n";
int pos;
cin>>pos;
int *value;
value=list1.Get(pos);
if(value)
cout<<"你要取出的元素为"<<*value<<endl;
else
cout<<"操作不成功,请重新输入";
break;
}
default:
cout<<"请按要求输入\n";
break;
}
cout<<"请选择你需要进行的操作:\n";
cout<<"1:插入一个新的节点\n";
cout<<"2:搜索含数据value的节点\n";
cout<<"3:搜索链表中第i个元素的地址\n";
cout<<"4:将新的元素插入到链表中的第i个位置\n";
cout<<"5:将链表中的第i个元素删除\n";
cout<<"6:取出链表中的第i个元素\n";
cout<<"0:退出\n";
cin>>choice;
}
return 0;
}
在编译的时候出现错误如下
------ 已启动生成: 项目: List, 配置: Debug Win32 ------
正在编译...
TestList.cpp
e:\c++程序\数据结构练习\list\list\testlist.cpp(10) : error C2248: 'List<Type>::List' : cannot access private member declared in class 'List<Type>'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\list.h(21) : see declaration of 'List<Type>::List'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\testlist.cpp(10) : error C2248: 'List<Type>::~List' : cannot access private member declared in class 'List<Type>'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\list.h(22) : see declaration of 'List<Type>::~List'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\testlist.cpp(20) : error C2248: 'List<Type>::InsertNode' : cannot access private member declared in class 'List<Type>'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\list.h(27) : see declaration of 'List<Type>::InsertNode'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\testlist.cpp(42) : error C2248: 'List<Type>::InsertNode' : cannot access private member declared in class 'List<Type>'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\list.h(27) : see declaration of 'List<Type>::InsertNode'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\testlist.cpp(51) : error C2248: 'List<Type>::Find' : cannot access private member declared in class 'List<Type>'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\list.h(26) : see declaration of 'List<Type>::Find'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\testlist.cpp(64) : error C2248: 'List<Type>::Find' : cannot access private member declared in class 'List<Type>'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\list.h(26) : see declaration of 'List<Type>::Find'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\testlist.cpp(76) : error C2248: 'List<Type>::InsertNode' : cannot access private member declared in class 'List<Type>'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\list.h(27) : see declaration of 'List<Type>::InsertNode'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\testlist.cpp(88) : error C2248: 'List<Type>::Remove' : cannot access private member declared in class 'List<Type>'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\list.h(28) : see declaration of 'List<Type>::Remove'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\testlist.cpp(101) : error C2248: 'List<Type>::Get' : cannot access private member declared in class 'List<Type>'
with
[
Type=int
]
e:\c++程序\数据结构练习\list\list\list.h(29) : see declaration of 'List<Type>::Get'
with
[
Type=int
]
生成日志保存在“file://e:\C++程序\数据结构练习\List\List\Debug\BuildLog.htm”
List - 9 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
这是怎么回事,为什么都是不能获取类的私有成员呢?
搜索更多相关主题的帖子: 函数 Type ListNode class 节点 
2007-10-21 15:06
张学靖
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2007-10-18
收藏
得分:0 

看不懂


2007-10-22 13:38
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
友员函数不能访问是vc6.0的一个bug,你换个编译器试试。

没看代码,也可能有其他错误!

Fight  to win  or  die...
2007-10-22 19:33
栖柏
Rank: 2
等 级:论坛游民
威 望:3
帖 子:1103
专家分:17
注 册:2007-8-23
收藏
得分:0 
vc6.0中的try模块也有问题

You have lots more to work on! Never give up!c language!
2007-10-22 19:34
Kid_X
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:216
专家分:515
注 册:2007-10-8
收藏
得分:0 
我看到一个问题,定义List类时里面应加个public
class定义类默认的好像是 private的
我也不是很清楚
你可以试试看
2007-10-23 12:51
快速回复:友元函数的问题
数据加载中...
 
   



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

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