c++ - 链接器提示 vtable 上的 undefined reference

标签 c++ linker undefined vtable

我的 fileType.h 中有这段代码。

class FileType{
    private:
        School* m_school;
        string m_fileFormat;
        const string m_cfgFile;
        const string m_inputFile;

    public:
        FileType(string p_fileFormat, string p_cfgFile, string p_inputFile):
                 m_fileFormat(p_fileFormat), m_cfgFile(p_cfgFile), m_inputFile(p_inputFile) {};
        virtual bool parseInputFile();
        virtual bool writeOutputFile(const School& m_school);
        virtual bool checkFormat(); // TBD -- used to check the format of the input file
        virtual bool checkConstraints(); // TBD -- used to check things like only +ve 12 digit in the ID etc
        virtual ~FileType();
};

class XmlType:public FileType{

    public:
        XmlType(string p_fileFormat, string p_cfgFile, string p_inputFile):
            FileType(p_fileFormat, p_cfgFile, p_inputFile) {};
        virtual bool parseInputFile();
        virtual bool writeOutputFile(const School& m_school);
        virtual bool checkFormat(); // TBD -- used to check the format of the input file
        virtual bool checkConstraints(); // TBD -- used to check things like only +ve 11 digit in the ID etc

};


class CsvType:public FileType{

    public:
        CsvType(string p_fileFormat, string p_cfgFile, string p_inputFile):
            FileType(p_fileFormat, p_cfgFile, p_inputFile) {};
        virtual bool parseInputFile();
        virtual bool writeOutputFile(const School& m_school);
        virtual bool checkFormat(); // TBD -- used to check the format of the input file
        virtual bool checkConstraints(); // TBD -- used to check things like only +ve 11 digit in the ID etc

};

在我的主要我投入:

#include "fileType.h"

    FileType *inputFilePtr, *outputFilePtr;
    string stringOne, stringTwo, stringThree;
    inputFilePtr = new CsvType(stringOne, stringTwo, stringThree);

现在我的链接器告诉我我不知道构造函数的符号:

/cygdrive/c/Users/Owner/AppData/Local/Temp/cclieUAi.o:main.cpp:(.text$_ZN7CsvTypeC1ESsSsSs[CsvType::CsvType(std::basic_string, std::allocator >, std: :basic_string, std::allocator >, std::basic_string, std::allocator >)]+0x1bf): undefined reference vtable for CsvType' /cygdrive/c/Users/Owner/AppData/Local/Temp/cclieUAi.o:main.cpp:(.text$_ZN8FileTypeC2ESsSsSs[FileType::FileType(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)]+0x3a): undefined reference to文件类型的 vtable' collect2: ld 返回 1 个退出状态

我尝试用两个整数创建一个虚拟构造函数,但什么都不做。那行得通,但是一旦我输入字符串,它就失败了。知道哪里出了问题吗?

最佳答案

您只定义了构造函数,但没有定义虚函数。仅仅声明它们是不够的。您还必须定义它们,这意味着函数应该有函数体,包含一些语句(尽管它也可以是空白的)。

//file.h
class A
{ 
   virtual void f(); //this is called declaration
};

//file.cpp
void A::f() //this is called definition
{
  //this is called function-body.
}

关于c++ - 链接器提示 vtable 上的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7665490/

相关文章:

php - 未定义索引 : query in, 将 mysql 转换为 mysqli

c++ - 使用 Qt 的文件列表和空结果

c++ - Linux 中是否可以检测 USB 扩展卡的存在?

c++ - 成员函数的正确实现方式

c++ - std::fill 的 Clang 静态参数导致链接器失败

linker - 有没有办法确保 clang 链接未引用的库符号

typescript - 为什么 TypeScript 不能确定在检查未定义返回的 if 语句之后定义了变量

c++ - 如何在 Windows 上使用 g2o 框架进行图形优化 (SLAM)?

c++访问类的私有(private)数据

node.js - 在nodejs中运行Neo4j查询返回 "undefined"