C2491: 'std::numpunct<_elem>::id' : 不允许定义 dllimport 静态数据成员

发布于 2024-12-20 07:15:50 字数 489 浏览 1 评论 0原文

给出以下代码,

#include <sstream>
#include <stdint.h>

template <typename D> void func() {
    std::basic_stringstream<D> outStream;
    D suffix = 0;
    outStream << suffix;
}

void main() {
    func<char>();     // OK
    func<wchar_t>();  // OK
    func<uint16_t>(); // generates C2491
}

以下编译错误意味着什么?

错误 C2491:“std::numpunct<_Elem>::id”:不允许定义 dllimport 静态数据成员

Given following code,

#include <sstream>
#include <stdint.h>

template <typename D> void func() {
    std::basic_stringstream<D> outStream;
    D suffix = 0;
    outStream << suffix;
}

void main() {
    func<char>();     // OK
    func<wchar_t>();  // OK
    func<uint16_t>(); // generates C2491
}

what does following compile error mean?

error C2491: 'std::numpunct<_Elem>::id' : definition of dllimport static data member not allowed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒儿 2024-12-27 07:15:50

您不能声明方法

_declspec(dllimport)

并为其提供定义。

限定符告诉编译器该函数是从与您现在正在编译的库不同的库导入的,因此为其提供定义是没有意义的。

当包含标头时,限定符应该是

_declspec(dllimport)

,并且当您编译提供方法定义的模块时,它应该是:

_declspec(dllexport)

通常的方法是:

#ifdef CURRENT_MODULE
#define DLLIMPORTEXPORT _declspec(dllexport)
#else
#define DLLIMPORTEXPORT _declspec(dllimport)
#endif

定义CURRENT_MODULE仅在包含定义的模块,因此在编译该模块时会导出该方法。包含标头的所有其他模块均未定义 CURRENT_MODULE,并且将导入该函数。

我猜你的指令 - _declspecimport - 与此类似。

You can't declare methods with

_declspec(dllimport)

and provide a definition for them.

The qualifier tells the compiler that the function is imported from a different library than the one you are compiling now, so it wouldn't make sense to provide a definition for it.

When including the header, the qualifier should be

_declspec(dllimport)

and when you are compiling the module that provides a definition for the method it should be:

_declspec(dllexport)

The usual way of doing this is:

#ifdef CURRENT_MODULE
#define DLLIMPORTEXPORT _declspec(dllexport)
#else
#define DLLIMPORTEXPORT _declspec(dllimport)
#endif

The define CURRENT_MODULE is only defined in the module that contains the definitions, so when compiling that module the method is exported. All other modules that include the header don't have CURRENT_MODULE defined and the function will be imported.

I'm guessing your directive - _declspecimport - is similar to this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文