Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7562015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:18:28+00:00 2026-05-30T13:18:28+00:00

In the Bird class, there is one virtual function canFly() which is implemented in

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:18:30+00:00Added an answer on May 30, 2026 at 1:18 pm

    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[]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which are the guidelines for choosing between template duck-typing and pure virtual base class
Assume I have a Model class called Bird and a instance of Bird called
Scroll.js is used for #bird block, which follows the page scrolling. The main problem
public class Bird { private static int id = 0; private String kind; public
is there any way by which I can change an id type to NSString
I have the following Bird definition: class Bird: def __init__(self, swarm, position = None):
Is there a concept in C# of class definition and implementation similar to what
I am using Bird style literate haskell, which requires all code to be like
If I have a class called Animal, and a derived class Bird : public
public class Bird { private static int id = 0; private String kind; public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.