错误:"功能"是私有的,错误:在这种情况下

时间:2017-03-18 05:13:39

标签: c++

我是C ++的新手,我想知道为什么我的程序给了我这个错误。我构建了一个模拟兔子殖民地的程序。能够自动添加它们,给它们命名,年龄等。

这些是我得到的错误:

main2.cpp: In function ‘int main()’:
main2.cpp:109:6: error: ‘void Bunny::printBunny()’ is private
 void printBunny()
      ^
main2.cpp:132:26: error: within this context
   colony[ i ].printBunny();
                          ^

这是我提出的代码。

enter code here
#include <iostream>
#include <string>
#include <ctime> 
#include <vector>
#include <cstdlib>

using namespace std;

void setSex( void );
char getSex();
void setColor( void );
string getColor();
void setAge( void );
int getAge();
void setName( void );
string getName();
void printBunny();

static const int  POSSIBLE_NAMES = 5;
static const int  POSSIBLE_COLORS = 4;

class Bunny
{
    char sex;
    string color;
    int age;
    string name;

    bool radioactive_mutant_vampire_bunny;

    BunnyData()
    {
        //srand( time( 0 ) );

        setSex();
        setColor();
        setAge();
        setName();
    }

    void setSex()
    {
        int randomNumber = 1 + rand() % 2;

        ( randomNumber == 1 ) ? sex = 'm' : sex = 'f';
    }

    char getSex() 
    {
        return sex;
    }

    void setColor()
    {
        //color = possibleColors[ 0 + rand() % POSSIBLE_COLORS ];
    }

    string getColor() 
    {
        return color;
    }

    void setAge()
    {
        age = 0;
    }

    int getAge() 
    {
        return age;
    }

    void setName()
    {
        //name = possibleNames[ 0 + rand() % POSSIBLE_NAMES ];
    }

    string getName() 
    {
        return name;
    }

    void printBunny() 
    {
        cout << "Name: " << getName() << endl;
        cout << "Sex: " << getSex() << endl;
        cout << "Color: " << getColor() << endl;
        cout << "Age: " << getAge() << endl;
    }
};

int main()
{

    vector< Bunny > colony;

    cout << "Welcome to Bunny Graduation!" << endl << endl;

    for( int i = 0; i < 5; ++i )
    {
        colony.push_back( Bunny() );
    }

    for( int i = 0; i < 5; ++i )
    {
        colony[ i ].printBunny();
        cout << endl;
    }

    return 0;
}

3 个答案:

答案 0 :(得分:1)

你需要声明它们是公共的,如果你不这样做,这些成员默认是私有的,这意味着你不能在课外调用它们。

class Bunny
{public:
void printBunny() {...}
};

您确实需要考虑应该应用于成员的类访问修饰符,因为通常情况下,我们不会在OO中仅使用一种类型的访问修饰符。

您可以查看this site以了解详情。

答案 1 :(得分:1)

除非另有说明,否则班级中的所有内容都有私人访问权限。例如,

class access
{
    int iAmPrivate; // private. Not accessible outside of the class
public:
    int getValue() //public and accessible to all 
    {
        return iAmPrivate;
    }
    int iAmPublic; //also public
}

Documentation on access levels

答案 2 :(得分:0)

在你班上:

class Bunny
{
    char sex;
    string color;
    int age;
    string name;
    // Rest of class fields ...
}

默认情况下,所有字段都是私有的。这是因为在声明类public:的字段之前,您尚未使用关键字Bunny。因此,您无法在课程外调用/调用这些字段和函数,如您所尝试的那样:

colony[ i ].printBunny(); // you are calling a private member of class Bunny outside of the class

有两种方法可以解决此错误:

单向是您可以将班级成员声明为public。这将允许在类 之外调用函数和字段:

class Bunny
{
    public: // declare all fields of the class as public
        char sex;
        string color;
        int age;
        string name;
        // Rest of class fields ...
}

您可以做的另一件事是将Bunny声明为结构而不是类。这样,您的所有字段都可以在结构外部访问:

struct Bunny
{
    char sex;
    string color;
    int age;
    string name;
    // Rest of class fields ...
}
相关问题