c++中的继承不起作用.编译以下c++代码时发生链接器错误

Inheritance in C++ not working. Linker error occured while compiling the below C++ code

本文关键字:c++ 链接 错误 代码 继承 不起作用 编译      更新时间:2023-10-16

我正在尝试编译一个c++程序。我正在学习"虚函数"的概念。所以我试着看看当我们重写一个函数而不使其为"虚"时会发生什么。
我有三个目录头,源和对象。
在头目录中,我有2个文件,Pokemon.h和Charmander.h。在源代码目录中,我有3个文件Pokemon.cpp, charmand .cpp和Main.cpp。
Charmander.h中的类Charmander继承自Pokemon.h中的类Pokemon。

这是"headers"目录下的Pokemon.h:

/********************************************************************************
 * Pokemon --                                                                   *
 * This is the base class for all pokemons. Every pokemon will inherit from     *
 * this class.                                                                  *
 *                                                                              *
 * Author -- Aditya R.Singh                                                     *
 * Version -- 1.0                                                               *
 * Since -- 2014-06-21                                                          *
 ********************************************************************************/ 

#ifndef POKEMON_H
#define POKEMON_H

using namespace std;

class Pokemon {
    public:
        string type();
        string attack();
        string weakness();
};    

#endif   

这是"headers"目录下的Charmander.h:

/*******************************************************************************
 * Charmander --                                                               *
 * This class is the derived class from class Pokemon. This is specialized for *
 * the pokemon Charmander.                                                     *
 *                                                                             *
 * Author -- Aditya R.Singh                                                    *
 * Version -- 1.0                                                              *
 * Since -- 2014-06-18                                                         *
 *******************************************************************************/

#ifndef CHARMANDER_H
#define CHARMANDER_H

#include "Pokemon.h"  

using namespace std;

class Charmander: public Pokemon {
    public:
        string type();
        string attack();
        string weakness();
};    

#endif  

这是"sources"目录下的Pokemon.cpp:

/**********************************************************************************
 * Pokemon --                                                                     *
 * This is the implementation of functions in class Pokemon.                      *
 * This is a generic class for every pokemon.                                     *
 *                                                                                *
 * Author -- Aditya R.Singh                                                       *
 * Since -- 1.0                                                                   *
 * Version -- 2014-06-21                                                          *
 **********************************************************************************/

#include <iostream>
#include "../headers/Pokemon.h"

using namespace std;

string Pokemon::type() {
    return "Normal";
}    

string Pokemon::attack() {
    return "Headbutt";
}

string Pokemon::weakness() {
    return "Fighting";
}   

这是"sources"目录下的Charmander.cpp文件:

/**********************************************************************************
 * Charmander --                                                                  *
 * This is the Charmander class's functions implementations.                      *
 * This is specific to Charmander.                                                *
 *                                                                                *
 * Author -- Aditya R.Singh                                                       *
 * Since -- 1.0                                                                   *
 * Version -- 2014-06-21                                                          *
 **********************************************************************************/

#include <iostream>
#include "../headers/Charmander.h"

using namespace std;

string Charmander::type() {
    return "Fire";
} 

string Charmander::attack() {
    return "Fireflame";
}

string Charmander::weakness() {
    return "Water";
}  

这是"sources"目录下的Main.cpp文件:

/**********************************************************************************
 * Main --                                                                        *
 * This program will show the use of Polymorphism and Inheritance in C++.         *
 *                                                                                *
 * Author -- Aditya R.Singh                                                       *
 * Since -- 1.0                                                                   *
 * Version -- 2014-06-21                                                          *
 **********************************************************************************/

#include <iostream>
#include "../headers/Pokemon.h"
#include "../headers/Charmander.h"

using namespace std;

int main() {
    Pokemon *charmander = new Charmander;   

    cout << "Charmander is a " << charmander->type() << " type pokemon." << endl;
    cout << "Charmander can do a " << charmander->attack() << " attack." << endl;
    cout << "Charmander is weak against " << charmander->weakness() << " type pokemon." << endl;

    return 0;
}  

现在我编译了Pokemon.cpp,并把object文件放在"objects"目录下…目前我的终端在"Program"目录下…

gcc -c sources/Pokemon.cpp -o objects/Pokemon.o  

我编译了Charmander.cpp,并把object文件放在"objects"目录下…像这样…

gcc -c sources/Charmander.cpp -o objects/Charmander.o  

到现在为止一切正常。现在我在"objects"目录下有了目标文件。

但是现在我试着像这样编译Main.cpp ..

gcc sources/Main.cpp -o Main objects/Pokemon.o objects/Charmander.o  

但是我的GCC编译器给出了一些大的错误堆栈,比如架构x86_64的未定义符号。

我用的是mac book pro, 64位机。OS Mac OS X mavericks。

您应该使用g++而不是gcc来编译和链接c++程序,以便c++库得到正确的链接。

详情见https://stackoverflow.com/a/5854712/12711

要获得多态行为,基类必须至少有一个虚函数。

添加Pokemon的职业定义:

virtual ~Pokemon() { }

注意,由于您的其他函数不是虚函数,charmander->type()将调用Pokemon::type(),而不是Charmander::type()

NB。你的代码为我正确编译,添加了#include <string>。如果您仍然有问题,那么尝试将所有内容移到相同的目录,以防您可能有一个单元从不同的目录中拾取旧版本的头文件。