【发布时间】:2023-04-01 12:26:01
【问题描述】:
我只是想让我的代码编译。我以前做过这个,它的方法看起来完全一样,但是由于某种原因,当我尝试使用不同的方法运行它时,它不会编译。错误在 cpp 文件中。任何帮助都会很棒!谢谢
错误是:
/tmp/ccexQEF7.o: In function `Animal::Animal(std::string)':
Animal.cpp:(.text+0x11): undefined reference to `vtable for Animal'
collect2: error: ld returned 1 exit status
这是我的头文件:
#include <iostream>
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal
{
public:
Animal(std::string name);
std::string get_name();
virtual int get_weight();
virtual int get_age();
protected:
std::string animalName;
};
class Cat: public Animal
{
public:
Cat(double weight, int age);
std::string get_name();
virtual int get_age();
virtual int get_weight();
protected:
std::string catType;
};
#endif
这是我的 cpp 文件:
#include <iostream>
#include "Animal.h"
using namespace std;
Animal::Animal(string name)
{
animalName = name;
};
【问题讨论】:
-
你还需要实现剩余的功能:
std::string get_name();,virtual int get_weight();,virtual int get_age(); -
这能回答你的问题吗? Undefined reference to vtable
-
谢谢,但通常即使没有实现功能(可以稍后完成),即使输出什么也没有,代码仍然应该编译。
-
如果它们是虚拟的就不行。
-
创建可执行文件有两个步骤。首先你编译每个源文件;为每个源文件创建一个目标文件。然后您链接 目标文件和任何必要的库。这将创建可执行文件。问题中的错误消息来自链接器。您可以看到,因为错误消息提到“/tmp/ccexQEF7.o”。 “.o”是目标文件的扩展名。简而言之:编译好的代码,但没有链接,因为链接器找不到 vtable。 g++ 将 vtable 与其中一个虚函数一起放入。没有它们,代码将无法链接。
标签: c++ class inheritance virtual-functions function-definition