2011-10-05 43 views
0

我在我的fileType.h中有這段代碼。鏈接器抱怨關於vtable的undefined參考

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_st環,性病::分配器>,的std :: basic_string的,標準::分配器>)] + 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 to虛函數表爲文件類型」 collect2:LD返回1個退出狀態

我試圖使虛設構造有兩個整數,什麼都不做。這工作,但一旦我把字符串,這失敗了。任何想法有什麼不對?

+0

可能重複[未定義引用的vtable(http://stackoverflow.com/questions/3065154/undefined-reference-to-vtable) –

回答

2

您只定義了構造函數,但您尚未定義虛函數。只宣佈它們是不夠的。你也必須定義它們,這意味着函數應該有函數體,包含一些語句(儘管它也可以是空白的)。

//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. 
} 
+0

我做將FileType.cpp中的空函數放在FileType類中。 FileType ::〜FileType(){ delete m_school; } 布爾文件類型:: parseInputFile(){} 布爾writeOutputFile(){} 布爾checkFormat(){} 布爾CHECKCONSTRAINTS(){} 布爾CsvType :: parseInputFile() { } – Moto

+1

@Moto您確定要將程序與fileType.cpp連接嗎? – zdan

+0

@Moto:你沒有放過它們。只需再檢查一次! – Nawaz

0

你宣佈虛擬析構函數,但你的子類沒有!

哪裏是virtual ~CsvType() { }virtual ~XmlType() {}

如果這是你可能會考慮設置FileTypeto的功能由單純virtual bool parseInputFile() = 0;虛擬,讓你無法構建的文件類型的對象的抽象基類。

而且virtual bool parseInputFile() {}後,您不需要的;