0

In the Bird class, there is one virtual function canFly() which is implemented in two classes: Parrot and Crow. G is a global class which remembers the no. of birds (i.e. either Crow or parrot) and then printBirds() prints the birds' ability to fly.

But i am getting some error due to undefined references. Can somebody please explain this. Why are these errors occuring and how to rectify the program to remove errors.

#include <iostream>
using namespace std;
class Bird
{
    bool abilityToFly;
    public:
    Bird()
    {
        abilityToFly=0;
    }
    bool getAbility()
    {
        return abilityToFly;
    }
    void setAbility(bool x)
    {
        abilityToFly=x;
    }
    virtual void canFly()
    {
        abilityToFly=0;
    }
};

class Crow: public Bird
{
     public:
    void canFly()
    {
        setAbility(1);
    }
};

class Parrot: public Bird
{
    public:
    void canFly()
    {
         setAbility(1);
    }
};

class G
{
    public:
    static int numBirds;
    static Bird *b[10];
    static void addBird(Bird bird)
    {
         b[numBirds]= &bird;
         numBirds++;
         if (numBirds>10)
         cout<<"Error in program";
    }
    static void printBirds()
    {
         for(int i=0;i<numBirds;i++)
         {
         cout<<"Bird "<<i<<"'s ability to fly"<<b[i]->getAbility();
         }
    }
 };
 int G::numBirds=0;

 int main()
 {
     Parrot p;
     p.canFly();
     Crow c;
     c.canFly();
     G::addBird(p);
     G::addBird(c);
     G::printBirds();
     return 0;
 }

The errors are:

 In function `main':
 undefined reference to `G::b'
 undefined reference to `G::b'
 undefined reference to `G::b'

The link to the code is: http://codepad.org/Mjpu4wFv

7
  • Maybe because b was never initialized? Commented Feb 24, 2012 at 8:34
  • addBird() is broken. change the function to take a reference as parameter. Commented Feb 24, 2012 at 8:38
  • I can't get the meaning of canFly() could you explain it? Commented Feb 24, 2012 at 8:41
  • @UmNyobe what do you mean by addBird() is broken? Commented Feb 24, 2012 at 8:45
  • @RogMatthews, addBird() is broken becaue you are passing bird by value as the funtion's parameter, it provides a local temp value, which will be freed after the funtion ends, and in the function, you uses this temp value's address b[numBirds]= &bird; so this will cause undefined behaviour Commented Feb 24, 2012 at 8:48

1 Answer 1

6

G::b is a static data member, you should initialize it.

int G::numBirds=0;
Bird * G::b[10];

Another error in your code, you should modify as follows:

static void addBird(Bird* bird)
{
    b[numBirds] = bird;
    numBirds++;
    if (numBirds>10)
    cout<<"Error in program";
}

and in main():

G::addBird(&p);
G::addBird(&c);

Otherwise the parameter you transfer in addBird is a temporary one, but you give the address of the temporary parameter to G::b[]

Sign up to request clarification or add additional context in comments.

2 Comments

Still the code will not run, because g is a pointer to an array, right?
@Tim - I think it's meant to be an array of pointers. Some brackets in the declaration would clarify that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.