Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的错误+;由于未定义引用而导致的程序错误_C++_Oop - Fatal编程技术网

C++ C+中的错误+;由于未定义引用而导致的程序错误

C++ C+中的错误+;由于未定义引用而导致的程序错误,c++,oop,C++,Oop,在Bird类中,有一个虚拟函数canFly(),它在两个类中实现:Parrot和Crow。G是一个全局类,它记住鸟的数量(即乌鸦或鹦鹉),然后打印鸟()打印鸟的飞行能力 但是由于未定义的引用,我得到了一些错误。有人能解释一下吗。为什么会发生这些错误,以及如何纠正程序以消除错误 #include <iostream> using namespace std; class Bird { bool abilityToFly; public: Bird() {

在Bird类中,有一个虚拟函数canFly(),它在两个类中实现:Parrot和Crow。G是一个全局类,它记住鸟的数量(即乌鸦或鹦鹉),然后打印鸟()打印鸟的飞行能力

但是由于未定义的引用,我得到了一些错误。有人能解释一下吗。为什么会发生这些错误,以及如何纠正程序以消除错误

#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;
 }
代码的链接是:

G::b是静态数据成员,您应该初始化它

int G::numBirds=0;
Bird * G::b[10];
代码中的另一个错误,应修改如下:

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

否则,您在addBird中传输的参数是临时参数,但是您将临时参数的地址提供给G::b[]

G::b是静态数据成员,您应该初始化它

int G::numBirds=0;
Bird * G::b[10];
代码中的另一个错误,应修改如下:

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

否则,您在addBird中传输的参数是临时参数,但您将临时参数的地址提供给G::b[]

可能是因为b从未初始化?
addBird()
已断开。更改函数以引用作为参数。我无法理解
canFly()
的含义,您能解释一下吗?@UmNyobe您所说的
addBird()
被破坏是什么意思?@RogMatthews,
addBird()
被破坏,因为您通过值传递
bird
作为函数的参数,它提供了一个本地温度值,函数结束后将被释放,在函数中,使用此临时值的地址
b[numBirds]=&bird因此这将导致未定义的行为,可能是因为b从未初始化?
addBird()
已损坏。更改函数以引用作为参数。我无法理解
canFly()
的含义,您能解释一下吗?@UmNyobe您所说的
addBird()
被破坏是什么意思?@RogMatthews,
addBird()
被破坏,因为您通过值传递
bird
作为函数的参数,它提供了一个本地温度值,函数结束后将被释放,在函数中,使用此临时值的地址
b[numBirds]=&bird
这会导致未定义的行为,但代码不会运行,因为g是指向数组的指针,对吗?@Tim-我认为它应该是指针数组。声明中的一些括号会澄清这一点。但代码不会运行,因为g是指向数组的指针,对吗?@Tim-我认为它应该是指针数组。宣言中的一些括号将澄清这一点。