【问题标题】:Linker complains about undefined reference on vtable链接器抱怨 vtable 上未定义的引用
【发布时间】:2011-10-05 17:53:12
【问题描述】:

我的 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): 未定义对 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 tovtable 的 FileType 引用 collect2: ld 返回 1 个退出状态

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

【问题讨论】:

标签: c++ linker undefined vtable


【解决方案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.
}

【讨论】:

  • 我确实在 FileType 类的 fileType.cpp 中放置了空函数。 FileType::~FileType() { 删除 m_school; } bool FileType::parseInputFile() { } bool writeOutputFile() { } bool checkFormat() { } bool checkConstraints() { } bool CsvType::parseInputFile() { }
  • @Moto 你确定你正在用 fileType.cpp 链接你的程序吗?
  • @Moto:你没有放它们。请再检查一遍!
  • 很抱歉,我忘记在g++中添加fileType.cpp,显然它不知道它:(
【解决方案2】:

您声明了虚拟析构函数,但您的子类没有!

virtual ~CsvType() { }virtual ~XmlType() {} 在哪里

如果这是一个抽象基类,您可以考虑通过virtual bool parseInputFile() = 0; 将 FileType 的函数设置为纯虚拟,这样您就无法构造 FileType 的对象。

virtual bool parseInputFile() {} 之后,您不需要;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2011-02-03
    相关资源
    最近更新 更多