Constructor cannot access private members of his class

The following error appears in Visual Studio 2008: error C2248: "City :: City": cannot access the private member declared in the "City" class. It seems that the constructor cannot access members of its class. Any idea what is going on? Here is the code:

I have it:

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

And this class:

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

What is used in this class:

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

And here is the AddTown function:

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
source share
3 answers

By default, the class access level is private. If you do not add the publication: before the constructor of Town it will be closed.

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

, . , - .

, 'number' .

, , - ++, , "m_number". - "_number".

'num' , , . , -, .

Python "self.number", "", .

, , .

+3

You specified a local variable for a function that hides a member variable:

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

Try this instead

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

-1
source

Source: https://habr.com/ru/post/1709954/


All Articles