c++中的继承不起作用.编译以下c++代码时发生链接器错误
Inheritance in C++ not working. Linker error occured while compiling the below C++ code
我正在尝试编译一个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>
。如果您仍然有问题,那么尝试将所有内容移到相同的目录,以防您可能有一个单元从不同的目录中拾取旧版本的头文件。
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- CMake-按正确顺序将项目与C运行时对象文件链接
- 从链接列表c++中删除一个项目
- 有根的二进制搜索树.保留与其父级的链接
- 读取文件的最后一行并输入到链接列表时出错
- 静态数据成员的问题-修复链接错误会导致编译器错误
- node-gyp 在 macOS 上未正确链接库
- 基于boost的程序的静态链接——zlib问题
- 无法链接 CMake 中的本地库
- 内联函数中具有内部链接的全局变量
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 使用gcc从静态链接的文件中查找可选符号
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 下面是我为检测链接列表中的循环而制作的代码
- 有了gcc,是否可以链接库,但前提是它存在
- 使用C链接在函数内部创建C++模板
- Visual Studio mkl_link_tool.exe链接错误
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 有人能帮我处理这个链接列表吗?C++