2013-03-10 45 views
0

這是我的錯誤類來處理與嘗試&捕獲錯誤:C++ - 與Error類編譯錯誤,從性病繼承::異常

#include <stdexcept> 
#include <string> 

    class Error : public std::exception 
    { 
    public: 
     Error(const std::string&) throw(); 
     ~Error() throw(); 
     const char* what() const throw(); 
    private: 
     std::string   _msg; 
    }; 

而且cpp文件:

#include "Error.hpp" 

Error::Error(const std::string& msg) throw() 
    : _msg(msg) 
{ 
} 

Error::~Error() throw() 
{ 
} 

const char*  Error::what() const throw() 
{ 
    return (_msg.c_str()); 
} 

而且我在編譯時遇到這個錯誤:

main.o:(.gcc_except_table+0x34): undefined reference to `typeinfo for Error' 
MailBox.o: In function `MailBox::MailBox(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': 
MailBox.cpp:(.text+0x245): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
MailBox.cpp:(.text+0x268): undefined reference to `Error::~Error()' 
MailBox.cpp:(.text+0x270): undefined reference to `typeinfo for Error' 
MailBox.cpp:(.text+0x2f0): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
MailBox.cpp:(.text+0x313): undefined reference to `Error::~Error()' 
MailBox.cpp:(.text+0x31b): undefined reference to `typeinfo for Error' 
MailBox.cpp:(.text+0x3d6): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
MailBox.cpp:(.text+0x3f9): undefined reference to `Error::~Error()' 
MailBox.cpp:(.text+0x401): undefined reference to `typeinfo for Error' 
MailBox.cpp:(.text+0x452): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
MailBox.cpp:(.text+0x475): undefined reference to `Error::~Error()' 
MailBox.cpp:(.text+0x47d): undefined reference to `typeinfo for Error' 
MailBox.cpp:(.text+0x50a): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
MailBox.cpp:(.text+0x52d): undefined reference to `Error::~Error()' 
MailBox.cpp:(.text+0x535): undefined reference to `typeinfo for Error' 
MailBox.cpp:(.text+0x6af): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
MailBox.cpp:(.text+0x6d2): undefined reference to `Error::~Error()' 
MailBox.cpp:(.text+0x6da): undefined reference to `typeinfo for Error' 
MailBox.cpp:(.text+0x854): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
MailBox.cpp:(.text+0x877): undefined reference to `Error::~Error()' 
MailBox.cpp:(.text+0x87f): undefined reference to `typeinfo for Error' 
MailBox.cpp:(.text+0x923): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
MailBox.cpp:(.text+0x946): undefined reference to `Error::~Error()' 
MailBox.cpp:(.text+0x94e): undefined reference to `typeinfo for Error' 
MailBox.cpp:(.text+0x9b6): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
MailBox.cpp:(.text+0x9d9): undefined reference to `Error::~Error()' 
MailBox.cpp:(.text+0x9e1): undefined reference to `typeinfo for Error' 
collect2: ld returned 1 exit status 

我已經使用這個Error類的另一個r項目,並且運作良好。我不明白爲什麼在這裏它不起作用。

回答

3

這是不是編譯錯誤,這是一個鏈接器錯誤。基本上,這個錯誤告訴你一些你的函數的定義丟失了。

從鏈接器的輸出中讀取,顯然這些函數是Error類的拷貝構造函數和析構函數。

這與您僅顯示這些函數的聲明(在Error的類定義中)相一致。您還應該爲他們提供定義。例如,您可以簡單地將這些定義內聯:

class Error : public std::exception 
{ 
public: 
    Error(const std::string& s) throw() : _msg(s) { } 
    ~Error() throw() { }; 
    const char* what() const throw() { return _msg.c_str(); }; 
private: 
    std::string _msg; 
}; 
+0

但是我已經有了該代碼的Error.cpp文件。 – Elfayer 2013-03-10 00:13:51

+0

請參閱我編輯了我的消息。 – Elfayer 2013-03-10 00:17:24

+2

@Elfayer:然後'Error.cpp'文件不會被編譯器以某種方式處理。檢查您的項目設置並確保文件不會從構建中排除。另外,爲了驗證這是否是問題,請按照我的建議嘗試內聯這些函數定義。如果它起作用,那麼問題肯定是'Error.cpp'沒有得到處理。 – 2013-03-10 00:21:16