【问题标题】:Error: "function" is private void, error: within this context错误:“函数”是私有无效,错误:在此上下文中
【发布时间】:2017-03-18 05:13:38
【问题描述】:

我对 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;
}

【问题讨论】:

标签: c++


【解决方案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】:

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

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

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

    您可以查看this site以了解更多信息。

    【讨论】:

    • 或者从 class Bunny 更改为 struct Bunny 并不管其他一切。
    【解决方案3】:

    在你的课堂上:

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

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

    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 ...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-26
      • 2020-04-26
      • 1970-01-01
      • 2012-11-20
      • 2013-04-27
      • 2014-04-19
      • 2012-08-13
      • 2017-09-04
      • 2022-08-04
      相关资源
      最近更新 更多