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

时间:2022-05-27 23:05:39

I get the following error in Visual Studio 2008: error C2248: 'Town::Town' : cannot access private member declared in class 'Town'. It looks like the constructor is unable to access the members of its own class. Any idea what's going on? Here's the code:

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

I have this:

我有这个:

template<class T> class Tree{...}

And this class:

而这堂课:

class Town{
    Town(int number):number(number){};
    ...
private: 
    int number;
};

Which is used in this class:

本课程使用的是:

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

And here's the AddTown function:

这是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;
}

3 个解决方案

#1


By default class access level is private. If you do not add a public: before the Town constructor it will be private.

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

class Town{
public: // <- add this
    Town(int number):number(number){};
    ...
private: 
    int number;
};

#2


This code had both the logic problems listed above. Both the non public declaration and the hiding of the member variables by using the same name passed as internal.

此代码具有上面列出的逻辑问题。非公开声明和隐藏成员变量都使用相同的名称作为内部传递。

Considering we are talking about the 'number' variable as an example.

考虑到我们所说的'数字'变量就是一个例子。

In the past such as in c++ member variables that were private internal variables would be prefixed with 'm_number'. Or some would do just '_number'.

在过去,例如在c ++成员变量中,私有内部变量将以'm_number'作为前缀。或者有人会做'_number'。

Just 'num' is a good rename but it may not be clear that it is just the same value. I suppose this is a problem of internal private member variable naming that hasn't really been solved.

只是'num'是一个很好的重命名,但可能并不清楚它是否只是相同的值。我想这是内部私有成员变量命名的问题,但尚未真正解决。

Python uses 'self.number' which would distinguish from 'number' passed in which is a decent solution.

Python使用'self.number'来区分传递的'number',这是一个不错的解决方案。

But whatever the platform, it is always good to have a private variable member naming system that helps you to avoid hacks at the points that you have name collisions.

但无论平台如何,拥有一个私有变量成员命名系统总是好的,它可以帮助您避免在名称冲突点发生攻击。

#3


You declared a local variable for the function that hides the member variable:

您为隐藏成员变量的函数声明了一个局部变量:

Town(int number):number(number){};

Try this instead

试试这个

Town(int num):number(num){};

#1


By default class access level is private. If you do not add a public: before the Town constructor it will be private.

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

class Town{
public: // <- add this
    Town(int number):number(number){};
    ...
private: 
    int number;
};

#2


This code had both the logic problems listed above. Both the non public declaration and the hiding of the member variables by using the same name passed as internal.

此代码具有上面列出的逻辑问题。非公开声明和隐藏成员变量都使用相同的名称作为内部传递。

Considering we are talking about the 'number' variable as an example.

考虑到我们所说的'数字'变量就是一个例子。

In the past such as in c++ member variables that were private internal variables would be prefixed with 'm_number'. Or some would do just '_number'.

在过去,例如在c ++成员变量中,私有内部变量将以'm_number'作为前缀。或者有人会做'_number'。

Just 'num' is a good rename but it may not be clear that it is just the same value. I suppose this is a problem of internal private member variable naming that hasn't really been solved.

只是'num'是一个很好的重命名,但可能并不清楚它是否只是相同的值。我想这是内部私有成员变量命名的问题,但尚未真正解决。

Python uses 'self.number' which would distinguish from 'number' passed in which is a decent solution.

Python使用'self.number'来区分传递的'number',这是一个不错的解决方案。

But whatever the platform, it is always good to have a private variable member naming system that helps you to avoid hacks at the points that you have name collisions.

但无论平台如何,拥有一个私有变量成员命名系统总是好的,它可以帮助您避免在名称冲突点发生攻击。

#3


You declared a local variable for the function that hides the member variable:

您为隐藏成员变量的函数声明了一个局部变量:

Town(int number):number(number){};

Try this instead

试试这个

Town(int num):number(num){};