构造函数无法访问其自己的类的私有成员

Eps*_*tor 3 c++ templates visual-studio-2008

我在Visual Studio 2008中收到以下错误:错误C2248:'Town :: Town':无法访问在类'Town'中声明的私有成员.看起来构造函数无法访问其自己的类的成员.知道发生了什么事吗?这是代码:

我有这个:

template<class T> class Tree{...}
Run Code Online (Sandbox Code Playgroud)

而这堂课:

class Town{
    Town(int number):number(number){};
    ...
private: 
    int number;
};
Run Code Online (Sandbox Code Playgroud)

本课程使用的是:

class Country{
public:
    StatusType AddTown(Shore side, int location, int maxNeighborhoods);
private:
    Tree<Town> towns[2];
    ...
}
Run Code Online (Sandbox Code Playgroud)

这是AddTown函数:

StatusType Country::AddTown(Shore side, int location, 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;
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*eas 12

默认情况下,类访问级别是私有的.如果你没有添加public:在Town构造函数之前它将是私有的.

class Town{
public: // <- add this
    Town(int number):number(number){};
    ...
private: 
    int number;
};
Run Code Online (Sandbox Code Playgroud)