0

I am fairly new to C++ and I am wanting to understand why my program is giving me this error. I constructing a program that will simulate a colony of bunnies. Being able to automatically add them, give them names, ages, etc.

These are the errors that I am getting:

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();
                          ^

Here is the code that I have come up with.

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;
}
  • What is not clear about the error message? Do you know the difference between `private`, `public`, `protected`? – PaulMcKenzie Mar 18 '17 at 05:17
  • [We had another user asking about lists of `Bunny`s earlier.](http://stackoverflow.com/questions/42869014/advice-on-how-to-resolve-this-error) If you are allowed to use `std::vector` for this assignment, wow. He's going to be pissed about all the time he's put into writing a linked list. – user4581301 Mar 18 '17 at 05:24

3 Answers3

2

You need to declare them to be public, if you don't, these members are private by default, which means you cannot invoke them outside the class.

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

You really need to consider which class access modifier you should apply to the member, because normally, we don't use just one type of access modifier in OO.

You can check this site to learn more about it.

Jiahao Cai
  • 1,222
  • 1
  • 11
  • 25
2

Everything in a class has private access unless stated otherwise For example,

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.

user4581301
  • 33,082
  • 7
  • 33
  • 54
0

In your class:

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

All the fields are private by default. This is because you have not used the keyword public: before you declare the fields of the class Bunny. As a result, you cannot invoke/call these fields and functions outside of the class, as you have tried in:

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

There are 2 ways to get around this error:

One way is that you can declare the class members as public. This will allow the functions and fields to be invoked outside of the class:

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

Another thing you can do is declare Bunny as a struct instead if a class. This way, all your fields sill be accessible outside the struct:

struct Bunny
{
    char sex;
    string color;
    int age;
    string name;
    // Rest of class fields ...
}
BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31