2012-08-09 78 views
0

我試圖通過擴展ctype <uint32_t>來創建自定義ctype構面,但不斷收到未定義的引用錯誤。嘗試擴展ctype構面時未定義的引用錯誤

下面是一個最小的測試程序重現錯誤:

#include <locale> 

using namespace std; 

class my_ctype : public ctype<uint32_t> { 
public: 
    explicit my_ctype(size_t __refs = 0); 

protected: 
    virtual ~my_ctype(); 

protected: 
    virtual bool do_is(mask __m, char_type __c) const; 
    virtual const char_type* do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const; 
    virtual const char_type* do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const; 
    virtual const char_type* do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const; 
    virtual char_type do_toupper(char_type __c) const; 
    virtual const char_type* do_toupper(char_type* __lo, const char_type* __hi) const; 
    virtual char_type do_tolower(char_type __c) const; 
    virtual const char_type* do_tolower(char_type* __lo, const char_type* __hi) const; 
    virtual char_type do_widen(char __c) const; 
    virtual const char* do_widen(const char* __lo, const char* __hi, char_type* __dest) const; 
    virtual char do_narrow(char_type __c, char __dfault) const; 
    virtual const char_type* do_narrow(const char_type* __lo, const char_type* __hi, char __dfault, char* __to) const; 
}; 

my_ctype::my_ctype(size_t __refs) : ctype<uint32_t>(__refs) {} 

my_ctype::~my_ctype() {} 

bool my_ctype::do_is(mask __m, char_type __c) const { return false; } 
const my_ctype::char_type* my_ctype::do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const { return __hi; } 
const my_ctype::char_type* my_ctype::do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const { return __hi; } 
const my_ctype::char_type* my_ctype::do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const { return __hi; } 
my_ctype::char_type my_ctype::do_toupper(char_type __c) const { return __c; } 
const my_ctype::char_type* my_ctype::do_toupper(char_type* __lo, const char_type* __hi) const { return __hi; } 
my_ctype::char_type my_ctype::do_tolower(char_type __c) const { return __c; } 
const my_ctype::char_type* my_ctype::do_tolower(char_type* __lo, const char_type* __hi) const { return __hi; } 
my_ctype::char_type my_ctype::do_widen(char __c) const { return __c; } 
const char* my_ctype::do_widen(const char* __lo, const char* __hi, char_type* __dest) const { return __hi; } 
char my_ctype::do_narrow(char_type __c, char __dfault) const { return __dfault; } 
const my_ctype::char_type* my_ctype::do_narrow(const char_type* __lo, const char_type* __hi, char __dfault, char* __to) const { return __hi; } 



int main() { 
    my_ctype* c = new my_ctype(); 

    return 0; 
} 

,這裏是錯誤我越來越:

Linking console executable: bin\Debug\ctype.exe 
obj\Debug\main.o: In function `_ZN8my_ctypeD2Ev': 
D:/[...]/ctype/main.cpp:29: undefined reference to `std::ctype<unsigned int>::~ctype()' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x8): undefined reference to `std::ctype<unsigned int>::~ctype()' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0xc): undefined reference to `std::ctype<unsigned int>::~ctype()' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x10): undefined reference to `std::ctype<unsigned int>::do_is(unsigned short, unsigned int) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x14): undefined reference to `std::ctype<unsigned int>::do_is(unsigned int const*, unsigned int const*, unsigned short*) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x18): undefined reference to `std::ctype<unsigned int>::do_scan_is(unsigned short, unsigned int const*, unsigned int const*) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x1c): undefined reference to `std::ctype<unsigned int>::do_scan_not(unsigned short, unsigned int const*, unsigned int const*) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x20): undefined reference to `std::ctype<unsigned int>::do_toupper(unsigned int) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x24): undefined reference to `std::ctype<unsigned int>::do_toupper(unsigned int*, unsigned int const*) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x28): undefined reference to `std::ctype<unsigned int>::do_tolower(unsigned int) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x2c): undefined reference to `std::ctype<unsigned int>::do_tolower(unsigned int*, unsigned int const*) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x30): undefined reference to `std::ctype<unsigned int>::do_widen(char) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x34): undefined reference to `std::ctype<unsigned int>::do_widen(char const*, char const*, unsigned int*) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x38): undefined reference to `std::ctype<unsigned int>::do_narrow(unsigned int, char) const' 
obj\Debug\main.o:main.cpp:(.rdata$_ZTVSt5ctypeIjE[vtable for std::ctype<unsigned int>]+0x3c): undefined reference to `std::ctype<unsigned int>::do_narrow(unsigned int const*, unsigned int const*, char, char*) const' 
collect2.exe: error: ld returned 1 exit status 
Process terminated with status 1 (0 minutes, 1 seconds) 
15 errors, 1 warnings 

我看着locale_facets.h,並發現有ctype的模板專門化<char>和ctype <wchar_t>,恐怕我可能不得不爲uint32_t創建專門化,但我只是不明白爲什麼。

因此,這裏是我的問題:

1 - 爲什麼我收到這些錯誤消息?

2 - 我該如何解決?

3 - 如果我確實需要爲uint32_t專門設置ctype < >,爲什麼會出現這種情況?

我正在使用mingw在windows上編譯我的程序,通過CodeBlocks。測試程序只有main.cpp,沒有其他文件。

+0

'std :: ctype'用於字符,你想要做什麼? – 2012-08-10 00:00:22

+0

@JesseGood我試圖爲unicode字符實現它。 – akamaru 2012-08-10 00:35:36

回答

1
  1. 由於沒有專業化的uint32_t的
  2. 寫一個專業化
  3. 基類必須實現

考慮下面

class A {
public:
virtual void func(); // no implementation here virtual ~A();
};
class B : public A
{
public:
virtual void func() {}
};

實例代碼的B無法創建。

+1

我想我知道了......類ctype <>中定義的方法既沒有實現,也沒有定義爲純虛擬,因此編譯器希望在另一個源文件的某處找到它們的實現,這種情況不會發生。所以唯一的解決方案是爲類ctype <>本身提供這些實現,而不僅僅是派生類。這意味着專門化ctype <>。 – akamaru 2012-08-10 00:46:44

+0

是的。僅供參考,即使所有函數都被標記爲純虛函數,但仍需要實現基類中的析構函數。 http://stackoverflow.com/questions/1219607/why-do-we-need-a-pure-virtual-destructor-in-c – silvesthu 2012-08-10 00:58:25

0

我的問題是沒有構造函數,希望能幫助別人!

相關問題