VC++2005 error C2248 cannot access private member declared in class >>>>在线等

leolovefun 2006-05-02 10:20:19
code:
--------------------------------list.h----------------------------------------
#ifndef LIST_H
#define LIST_H

#include "list_item.h"

template< class elemtype >
class list
{
list():_front(0), _end(0), _current(0), _size(0){}
...
~list() { remove_all() };
...
void insert_front( elemtype value );
...
private:
...
list_item< elemtype > *_front;
list_item< elemtype > *_end;
list_item< elemtype > *_current;
int _size;
};
#endif
-----------------------------------list.cpp---------------------------
#include "list.h"
#include <iostream>

template< typename elemtype >
void list< elemtype >::insert_front( elemtype value )
{
list_item *pflist = _front;
if( plist )
{
pflist->value( value );
pflist->next(_front);
_front = pflist;
}
}
-------------------------list_item.h---------------------------
#ifndef LIST_ITEM_H
#define LIST_ITEM_H

template< class elemtype >
class list_item
{
public:
list_item( elemtype value, list_item *item = 0 )
:_value( value )
{
if ( ! item )
_next = 0;
else
{
_next = item->_next;
item->_next = this;
}
}

elemtype value() { return _value; }
list_item* next() { return _next; }

void next( list_item *link ) { _next = link; }
void value ( elemtype value ) { _value = value; }
private:
elemtype _value;
list_item *_next;
};

#endif
------------------------------main.cpp---------------------------
#include "list.h"
#include <iostream>

int main()
{
list<int> mylist;

mylist.insert_front( 9 );
return 0;
}
---------------------------compile information----------------------

1>------ Build started: Project: list, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\documents and settings\leo\my documents\visual studio 2005\projects\list\list\main.cpp(6) : error C2248: 'list<elemtype>::list' : cannot access private member declared in class 'list<elemtype>'
1> with
1> [
1> elemtype=int
1> ]
1> c:\documents and settings\leo\my documents\visual studio 2005\projects\list\list\list.h(9) : see declaration of 'list<elemtype>::list'
1> with
1> [
1> elemtype=int
1> ]
1>c:\documents and settings\leo\my documents\visual studio 2005\projects\list\list\main.cpp(6) : error C2248: 'list<elemtype>::~list' : cannot access private member declared in class 'list<elemtype>'
1> with
1> [
1> elemtype=int
1> ]
1> c:\documents and settings\leo\my documents\visual studio 2005\projects\list\list\list.h(13) : see declaration of 'list<elemtype>::~list'
1> with
1> [
1> elemtype=int
1> ]
1>c:\documents and settings\leo\my documents\visual studio 2005\projects\list\list\main.cpp(8) : error C2248: 'list<elemtype>::insert_front' : cannot access private member declared in class 'list<elemtype>'
1> with
1> [
1> elemtype=int
1> ]
1> c:\documents and settings\leo\my documents\visual studio 2005\projects\list\list\list.h(17) : see declaration of 'list<elemtype>::insert_front'
1> with
1> [
1> elemtype=int
1> ]
1>Build log was saved at "file://c:\Documents and Settings\Leo\My Documents\Visual Studio 2005\Projects\list\list\Debug\BuildLog.htm"
1>list - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
请问为什么会有这个错误???
...全文
158 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
leolovefun 2006-05-02
  • 打赏
  • 举报
回复
Thanks
lightnut 2006-05-02
  • 打赏
  • 举报
回复
template< class elemtype >
class list
{
list():_front(0), _end(0), _current(0), _size(0){}
...
~list() { remove_all() };
...
void insert_front( elemtype value );
...

====>
template< class elemtype >
class list
{
//if no 'public' here, the default is 'private'. Thus gives rise to
//your compiling errors.
public:
list():_front(0), _end(0), _current(0), _size(0){}
...
~list() { remove_all() };
...
void insert_front( elemtype value );
...

64,654

社区成员

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

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