c – 构造函数无法访问其自己的类的私有成员
发布时间:2020-12-16 10:38:29 所属栏目:百科 来源:网络整理
导读:我在Visual Studio 2008中收到以下错误:错误C2248:’Town :: Town’:无法访问在类’Town’中声明的私有成员.看起来构造函数无法访问其自己的类的成员.知道发生了什么事吗? 这是代码: 我有这个: templateclass T class Tree{...} 而这堂课: class Town
我在Visual Studio 2008中收到以下错误:错误C2248:’Town :: Town’:无法访问在类’Town’中声明的私有成员.看起来构造函数无法访问其自己的类的成员.知道发生了什么事吗?
这是代码: 我有这个: template<class T> class Tree{...} 而这堂课: class Town{ Town(int number):number(number){}; ... private: int number; }; 本课程使用的是: class Country{ public: StatusType AddTown(Shore side,int location,int maxNeighborhoods); private: Tree<Town> towns[2]; ... } 这是AddTown函数: StatusType Country::AddTown(Shore side,int maxNeighborhoods){ if (maxNeighborhoods<0 || location<0){ return INVALID_INPUT; } Town* dummy= new Town(location);//Here be error C2248 if (towns[side].find(*dummy)!=NULL){ delete dummy; return FAILURE; } SouthBorder* dummyBorder; (side==NORTH)?dummyBorder=new SouthBorder(location,0):dummyBorder=new SouthBorder(0,location); if (southBorders.find(*dummyBorder)!=NULL){ delete dummyBorder; return FAILURE; } towns[side].add(*dummy); delete dummyBorder; return SUCCESS; } 解决方法
默认情况下,类访问级别是私有的.如果你没有添加public:在Town构造函数之前它将是私有的.
class Town{ public: // <- add this Town(int number):number(number){}; ... private: int number; }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |