gpt4 book ai didi

c++ - 尝试扩展 ctype facet 时出现 undefined reference 错误

转载 作者:行者123 更新时间:2023-11-28 07:59:25 24 4
gpt4 key购买 nike

我正在尝试通过扩展 ctype 创建自定义 ctype 方面,但不断出现 undefined reference 错误。

这是一个重现错误的最小测试程序:

#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 和 ctype 的模板特化,我担心我可能也必须为 uint32_t 创建一个特化,但我可以看看为什么。

所以这是我的问题:

1 - 为什么我会收到这些错误消息?

2 - 我该如何解决?

3 - 如果我必须为 uint32_t 专门化 ctype<>,为什么会这样?

我正在通过 CodeBlocks 在 Windows 上使用 mingw 编译我的程序。测试程序只有main.cpp,没有其他文件。

最佳答案

  1. 因为没有针对 uint32_t 的专门化
  2. 撰写专业
  3. 必须实现基类

考虑下面的代码


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

class B : public A<br/>
{<br/>
public:<br/>
virtual void func() {}<br/>
};

无法创建 B 的实例。

关于c++ - 尝试扩展 ctype facet 时出现 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11893499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com