[英]Inheritance in C++ not working. Linker error occured while compiling the below C++ code
I am trying to compile a C++ program. 我正在尝试编译C ++程序。 I am learning the "virtual functions" concept.
我正在学习“虚拟功能”的概念。 So I was trying to see what happens when we override a function without making it "virtual".
因此,我试图查看在不使函数成为“虚拟”函数的情况下发生的情况。
I have three directories headers, sources and objects. 我有三个目录头,源和对象。
In the header directory, I have 2 files, Pokemon.h and Charmander.h . 在头目录中,我有2个文件Pokemon.h和Charmander.h。 In the sources directory, I have 3 files Pokemon.cpp, Charmander.cpp and Main.cpp.
在sources目录中,我有3个文件Pokemon.cpp,Charmander.cpp和Main.cpp。
class Charmander in Charmander.h, inherits from class Pokemon from Pokemon.h. Charmander.h中的Charmander类继承自Pokemon.h中的Pokemon类。
This is Pokemon.h in the "headers" directory: 这是“ 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
This is Charmander.h in the "headers" directory: 这是“标头”目录中的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
This is the Pokemon.cpp in the "sources" directory: 这是“源”目录中的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";
}
This is the Charmander.cpp file in the "sources" directory: 这是“源”目录中的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";
}
And this is the Main.cpp file in the "sources" directory: 这是“源”目录中的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;
}
Now I compiled the Pokemon.cpp and put the object file in the "objects" directory... Currently my terminal is in the "Program" directory... 现在,我编译了Pokemon.cpp并将目标文件放在“对象”目录中...目前,我的终端在“程序”目录中...
gcc -c sources/Pokemon.cpp -o objects/Pokemon.o
I compiled the Charmander.cpp and the put the object file in the "objects" directory... like this .. 我编译了Charmander.cpp并将对象文件放到“对象”目录中。
gcc -c sources/Charmander.cpp -o objects/Charmander.o
It's working fine uptill now. 现在可以正常工作了。 I have the object files in the "objects" directory now.
我现在在“对象”目录中有目标文件。
But now I am trying to compile my Main.cpp like this.. 但是现在我试图像这样编译我的Main.cpp。
gcc sources/Main.cpp -o Main objects/Pokemon.o objects/Charmander.o
But my GCC compiler gives some big error stack like, undefined symbols for architecture x86_64. 但是我的GCC编译器给出了一些大错误堆栈,例如x86_64体系结构的未定义符号。
I am using a mac book pro, 64 bit machine. 我使用的是Macbook Pro,64位计算机。 OS Mac OS X maverics.
OS Mac OS X小牛。
To get polymorphic behaviour, the base class must have at least one virtual function. 为了获得多态行为,基类必须至少具有一个虚函数。
Add to the class definition of Pokemon: 添加到Pokemon的类定义中:
virtual ~Pokemon() { }
Note that since your other functions are not virtual, charmander->type()
will call Pokemon::type()
, not Charmander::type()
. 注意,由于其他函数不是虚函数,
charmander->type()
将调用Pokemon::type()
,而不是Charmander::type()
。
NB. 注意 Your code compiles correctly for me, with the addition of
#include <string>
. 通过添加
#include <string>
,您的代码可以为我正确编译。 If you still have problems then try moving everything to the same directory , in case you are perhaps having one unit pick up an old version of a header file from a different directory. 如果仍然有问题,请尝试将所有内容移至同一目录,以防万一您可能要从一个不同的目录中提取头文件的旧版本。
You should use g++
instead of gcc
to compile and link C++ programs so that the C++ library gets properly linked in. 您应该使用
g++
而不是gcc
来编译和链接C ++程序,以便正确地链接C ++库。
See https://stackoverflow.com/a/5854712/12711 for the gory details. 有关详细信息,请参阅https://stackoverflow.com/a/5854712/12711 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.