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

时间:2014-06-21 06:56:38

标签: c++ gcc linker-errors

我正在尝试编译C ++程序。我正在学习“虚拟功能”的概念。所以我试图看看当我们覆盖一个函数而不使它成为“虚拟”时会发生什么 我有三个目录标题,源和对象 在头文件目录中,我有2个文件,Pokemon.h和Charmander.h。 在sources目录中,我有3个文件Pokemon.cpp,Charmander.cpp和Main.cpp Charmander.h中的Charmander类,继承自Pokemon.h中的Pokemon类。

这是Pokemon.h在“headers”目录中:

/********************************************************************************
 * 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并将目标文件放在“objects”目录中......目前我的终端位于“Program”目录中......

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

我编译了Charmander.cpp并将目标文件放在“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 maverics。

2 个答案:

答案 0 :(得分:1)

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

添加到口袋妖怪的类定义:

virtual ~Pokemon() { }

请注意,由于您的其他功能不是虚拟的,charmander->type()会调用Pokemon::type(),而不是Charmander::type()

NB。您的代码正确编译,添加了#include <string>。如果您仍然遇到问题,请尝试将所有内容移动到同一目录,以防您有一个单元从另一个目录中获取旧版本的头文件。

答案 1 :(得分:1)

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

有关血腥的详细信息,请参阅https://stackoverflow.com/a/5854712/12711